📄 logmsg.cpp
字号:
// logmsg.cc// code for logmsg.h// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "logmsg.h" // this module#include "xassert.h" // xfailure#include "str.h" // stringb#include <fstream.h> // ofstream// define a sleep function#if defined(__WIN32__)#include <windows.h> // Sleep#define SHARING_MODE , filebuf::sh_readstatic void waitABit(){ Sleep(500); // 1/2 sec}static void failure(char const *filename){ MessageBox((HWND)NULL, stringb("Failure writing to logfile " << filename), "Log Failure", MB_OK);}#elif defined(__UNIX__)#include <unistd.h> // sleep// g++2.8.1 doesn't define filebuf::sh_read#define SHARING_MODEstatic void waitABit(){ sleep(1); // 1 sec}static void failure(char const *filename){ xfailure(stringb("Failure writing to logfile %s" << filename));}#else# error Must define a system type, e.g. __WIN32__ or __UNIX__#endifvoid LogMessage(char const* msg, char const *filename) { ofstream logfile; int retry=0; int success=0; // get a lock while (!success && retry < 10) { logfile.clear(); logfile.open(filename, ios::app SHARING_MODE); if (logfile.fail()) { waitABit(); retry++; } else success = 1; } if (!success) { failure(filename); return; // used only if 'failure' returns } // SM: wanted to see log contention approximation if (retry > 0) { logfile << "(after " << retry << " retries) "; } logfile << msg << endl; logfile.close(); }// ---------- test code --------------------------#ifdef TEST_LOGMSG#include <stdio.h> // printf#include "exc.h" // xBaseint main(){ printf("writing to tlog.log\n"); try { LogMessage("This is a sample log entry.", "tlog.log"); } catch (xBase &x) { return printf("exception caught: %s\n", x.why()); } return 0;}#endif // TEST_LOGGING
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -