网站运营单位是什么意思,怎么在腾讯云搭建wordpress,长沙公司做网站多少钱,重庆网站建设jccit以下示例展示了 log_agent 类#xff0c;它类似于 dataflow_agent 类。 log_agent 类实现异步记录代理#xff0c;用于将日志消息写入文件和控制台。 log_agent 类使应用程序能够将消息分类为信息性、警告或错误消息。 它还使应用程序能够指定每个日志类别是写入文件、控制台…以下示例展示了 log_agent 类它类似于 dataflow_agent 类。 log_agent 类实现异步记录代理用于将日志消息写入文件和控制台。 log_agent 类使应用程序能够将消息分类为信息性、警告或错误消息。 它还使应用程序能够指定每个日志类别是写入文件、控制台还是同时写入这两者。 本示例将所有日志消息写入文件并仅将错误消息写入控制台。
消息代理
// log-filter.cpp
// compile with: /EHsc
#include windows.h
#include agents.h
#include sstream
#include fstream
#include iostreamusing namespace concurrency;
using namespace std;// A synchronization primitive that is signaled when its
// count reaches zero.
class countdown_event
{
public:countdown_event(unsigned int count 0L): _current(static_castlong(count)) {// Set the event if the initial count is zero.if (_current 0L){_event.set();}}// Decrements the event counter.void signal(){if(InterlockedDecrement(_current) 0L){_event.set();}}// Increments the event counter.void add_count(){if(InterlockedIncrement(_current) 1L){_event.reset();}}// Blocks the current context until the event is set.void wait(){_event.wait();}private:// The current count.volatile long _current;// The event that is set when the counter reaches zero.event _event;// Disable copy constructor.countdown_event(const countdown_event);// Disable assignment.countdown_event const operator(countdown_event const);
};// Defines message types for the logger.
enum log_message_type
{log_info 0x1,log_warning 0x2,log_error 0x4,
};// An asynchronous logging agent that writes log messages to
// file and to the console.
class log_agent : public agent
{// Holds a message string and its logging type.struct log_message{wstring message;log_message_type type;};public:log_agent(const wstring file_path, log_message_type file_messages, log_message_type console_messages): _file(file_path), _file_messages(file_messages), _console_messages(console_messages), _active(0){if (_file.bad()){throw invalid_argument(Unable to open log file.);}}// Writes the provided message to the log.void log(const wstring message, log_message_type type){ // Increment the active message count._active.add_count();// Send the message to the network.log_message msg { message, type };send(_log_buffer, msg);}void close(){// Signal that the agent is now closed._closed.set();}protected:void run(){//// Create the dataflow network.//// Writes a log message to file.calllog_message writer([this](log_message msg){if ((msg.type _file_messages) ! 0){// Write the message to the file.write_to_stream(msg, _file);}if ((msg.type _console_messages) ! 0){// Write the message to the console.write_to_stream(msg, wcout);}// Decrement the active counter._active.signal();});// Connect _log_buffer to the internal network to begin data flow._log_buffer.link_target(writer);// Wait for the closed event to be signaled._closed.wait();// Wait for all messages to be processed._active.wait();// Close the log file and flush the console._file.close();wcout.flush();// Set the agent to the completed state.done();}private:// Writes a logging message to the specified output stream.void write_to_stream(const log_message msg, wostream stream){// Write the message to the stream.wstringstream ss;switch (msg.type){case log_info:ss Linfo: ;break;case log_warning:ss Lwarning: ;break;case log_error:ss Lerror: ;}ss msg.message endl;stream ss.str();}private: // The file stream to write messages to.wofstream _file;// The log message types that are written to file.log_message_type _file_messages;// The log message types that are written to the console.log_message_type _console_messages;// The head of the network. Propagates logging messages// to the rest of the network.unbounded_bufferlog_message _log_buffer;// Counts the number of active messages in the network.countdown_event _active;// Signals that the agent has been closed.event _closed;
};int wmain()
{// Union of all log message types.log_message_type log_all log_message_type(log_info | log_warning | log_error);// Create a logging agent that writes all log messages to file and error // messages to the console.log_agent logger(Llog.txt, log_all, log_error);// Start the agent.logger.start();// Log a few messages.logger.log(LLogging started., log_info);logger.log(LThis is a sample warning message., log_warning);logger.log(LThis is a sample error message., log_error);logger.log(LLogging finished., log_info);// Close the logger and wait for the agent to finish.logger.close();agent::wait(logger);
}
本示例将以下输出写入控制台。
error: This is a sample error message.
此示例还生成 log.txt 文件其中包含以下文本。
info: Logging started.
warning: This is a sample warning message.
error: This is a sample error message.
info: Logging finished.
编译代码
复制示例代码并将它粘贴到 Visual Studio 项目中或粘贴到名为 log-filter.cpp 的文件中再在 Visual Studio 命令提示符窗口中运行以下命令。
cl.exe /EHsc log-filter.cpp 文章转载自: http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.ddjp.cn.gov.cn.ddjp.cn http://www.morning.qmbpy.cn.gov.cn.qmbpy.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.xkyfq.cn.gov.cn.xkyfq.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.qlpyn.cn.gov.cn.qlpyn.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.ttdxn.cn.gov.cn.ttdxn.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.mzpd.cn.gov.cn.mzpd.cn http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.sjsfw.cn.gov.cn.sjsfw.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.phnbd.cn.gov.cn.phnbd.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.kszkm.cn.gov.cn.kszkm.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.bqhlp.cn.gov.cn.bqhlp.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.qmqgx.cn.gov.cn.qmqgx.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.mtjwp.cn.gov.cn.mtjwp.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.wzknt.cn.gov.cn.wzknt.cn http://www.morning.bpcf.cn.gov.cn.bpcf.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.thpzn.cn.gov.cn.thpzn.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.bszmy.cn.gov.cn.bszmy.cn http://www.morning.tdldh.cn.gov.cn.tdldh.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.jjpk.cn.gov.cn.jjpk.cn http://www.morning.ntcmrn.cn.gov.cn.ntcmrn.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.ygbq.cn.gov.cn.ygbq.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.xkjqg.cn.gov.cn.xkjqg.cn