void vlogMsg(int level, char* msg, va_list argp){ // Log the specified message, if its level is high enough if (level >= logLevel){ // The level (importance) of this message is sufficiently high that we want to log it FILE* logFile; if (logToFile == TRUE){ char* logPath = malloc(MAX_PATH_LEN); getLogPath(logPath); logFile = fopen(logPath, "a+"); if (logFile == NULL){ // The specified log file can't be accessed logToFile = FALSE; logMsg(LOG_ERR, "Unable to log to the file %s, logging to stdout instead", logPath); free(logPath); logMsg(level, msg); // log the original message return; } free(logPath); } else { if (level <= LOG_INFO){ logFile = stdout; } else { logFile = stderr; } } if (logToFile == TRUE){ // Only write a timestamp if we are logging to a file (as opposed to stdout/err) const time_t time = (time_t) getTime(); struct tm* cal = localtime(&time); int y = 1900 + cal->tm_year; int mo = 1 + cal->tm_mon; int d = cal->tm_mday; int h = cal->tm_hour; int mi = cal->tm_min; int s = cal->tm_sec; fprintf(logFile, "%d-%02d-%02d %02d:%02d:%02d ", y, mo, d, h, mi, s); if (appName != NULL){ fprintf(logFile, appName); fprintf(logFile, " "); } } // Write out the message, substituting the optargs for any tokens in the text vfprintf(logFile, msg, argp); fprintf(logFile, EOL); fflush(logFile); if (logToFile == TRUE){ fclose(logFile); } } }
Logger() { // Create the 1st appender. static plog::ColorConsoleAppender<plog::FuncMessageFormatter> consoleAppender; plog::init(plog::debug, &consoleAppender); plog::get()->setMaxSeverity(getLevel()); // Create the 2nd appender. static plog::RollingFileAppender<plog::FuncMessageFormatter> fileAppender(getLogPath().c_str(), getMaxFileSize(), getMaxFiles()); plog::get()->addAppender(&fileAppender); // Create the 3nd appender. static plog::OutputDebugStringAppender<plog::FuncMessageFormatter> OutputDebugStringAppender; // Create our custom appender. plog::get()->addAppender(&OutputDebugStringAppender); }
void show_error_log() { end_log(); #ifdef IS_WINDOWS system( getLogPath() ); #else FILE* f = open_log("r"); if(f != NULL) { fprintf(stderr, "\nLog:\n"); int c; while((c = getc(f)) != EOF) { putc(c, stderr); } putc('\n', stderr); fclose(f); } end_log(); #endif exit( -1 ); }
FILE* open_log(const char * mode) { end_log(); return fopen(getLogPath(), mode); }
void Log::open() { file = fopen(getLogPath().c_str(), "w"); }