📄 log.h
字号:
/*************************************************************************** DSemu - The Next Generation ** Stream-based event logging [log.h] ** Copyright Imran Nazar, 2005; released under the BSD public licence. ***************************************************************************/#ifndef __LOG_H_#define __LOG_H_#include <iostream>#include <fstream>#include <string>#include <time.h>#include "w32compile.h"#ifdef COMPILING_ON_WINDERZ #include <windows.h>#else #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <pwd.h> #include <errno.h>#endif#include "err.h"#include "defs.h"// If an error occurs, this exception will be thrown.#define ERR_LOG_INIT 0x0011class Logger{private: // Emulate a stream by overloading << // ACK: Kniht/EFnet#C++ (Stream-based logging idea and code) struct lg { std::ofstream &out; std::string module; template< typename T > lg &operator<<( T const &val) { out<<val; return *this; } lg(std::ofstream &out, std::string module) : out(out), module(module) { out << std::endl << stamp() << ": " << module << ": " ; } ~lg() { out << std::flush; } std::string stamp() { struct tm *ti; time_t epochtime; char str[32]; time(&epochtime); // Get the number of seconds since 1970 ti = localtime(&epochtime); // and convert to a date/time structure sprintf(str, "[%04d%02d%02d %02d:%02d:%02d]", ti->tm_year + 1900, // tm_year is since 1900 ti->tm_mon+1, // tm_mon is 0-11 ti->tm_mday, ti->tm_hour, ti->tm_min, ti->tm_sec); return std::string(str); } }; // Work out where to put our log file static const char* logname() { static char filename[MAX_PATH+20]; char tempname[MAX_PATH+20]; // Temporary storage for the function #ifdef WIN32 int i, len; if(!GetModuleFileName(NULL, tempname, MAX_PATH)) // Get path to the exe throw Exception(ERR_LOG_INIT, "Logger", "Unable to get module filename"); len = strlen(tempname); for(i = len; i; i--) if(tempname[i] == '\\') break; // Search backwards for backslash tempname[i] = 0; // End string before exe name sprintf(filename, "%s\\emulator.log", tempname); #else struct passwd *pwentry; pwentry = getpwuid(getuid()); // Retrieve passwd entry for user snprintf(tempname, MAX_PATH, "%s/.emulator", pwentry->pw_dir); mkdir(tempname, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); // Create directory under ~ if(errno && (errno != EEXIST)) // If there was an error that wasn't // 'directory already exists' throw Exception(ERR_LOG_INIT, "Logger", "Unable to create directory"); snprintf(filename, MAX_PATH+20, "%s/emulator.log", tempname); #endif return filename; }public: static std::string getFilename() { static std::string file = logname(); return file; } static void clean() { remove(logname()); } static lg log(std::string module) { // Only one stream is ever created, but transient lg's are // made, each putting their own timestamp down. static std::ofstream of(logname(), std::ios::out|std::ios::app); return lg(of, module); }};#endif//__LOG_H_/*** EOF: log.h **********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -