/* Conditional printf wrapper */ void cprintf(enum debug_level level, const char *fmt, ...) { va_list arg; if(level <= get_debug()) { va_start(arg, fmt); vfprintf(get_log_file(), fmt, arg); va_end(arg); } fflush(get_log_file()); }
static void get_log_by_type(const char *despath, const struct log_t *log, const char *srcpath) { if (!despath || !log || !srcpath) return; if (!strcmp("file", log->type)) { int lines; if (!log->lines) lines = 0; else if (cfg_atoi(log->lines, log->lines_len, &lines) == -1) return; get_log_file(despath, srcpath, lines); } else if (!strcmp("node", log->type)) { int size; if (!log->sizelimit) size = 0; else if (cfg_atoi(log->sizelimit, log->sizelimit_len, &size) == -1) return; get_log_node(despath, log->path, (size_t)(size * 1024 * 1024)); } else if (!strcmp("cmd", log->type)) get_log_cmd(despath, log->path); }
static BOOL CALLBACK config_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam) { char name[MAX_PATH+1]; int n; const char *s; (void)lparam; switch (msg) { case WM_INITDIALOG: center_window (dlg, 0); s = get_log_file (); SetDlgItemText (dlg, IDC_DEBUG_LOGFILE, s? s:""); config_dlg_set_labels (dlg); break; case WM_COMMAND: switch (LOWORD (wparam)) { case IDOK: n = GetDlgItemText (dlg, IDC_DEBUG_LOGFILE, name, MAX_PATH-1); set_log_file (n>0?name:NULL); EndDialog (dlg, TRUE); break; } break; } return FALSE; }
int Quotes_PrebuildContactMenu(WPARAM wp, LPARAM) { Menu_EnableItem(g_hMenuEditSettings, false); Menu_EnableItem(g_hMenuOpenLogFile, false); #ifdef CHART_IMPLEMENT Menu_EnableItem(g_hMenuChart,false); #endif Menu_EnableItem(g_hMenuRefresh, false); MCONTACT hContact = MCONTACT(wp); if (NULL == hContact) { return 0; } Menu_EnableItem(g_hMenuEditSettings, true); Menu_EnableItem(g_hMenuRefresh, true); tstring sLogFileName; bool bThereIsLogFile = (true == get_log_file(hContact, sLogFileName)) && (false == sLogFileName.empty()) && (0 == _taccess(sLogFileName.c_str(), 04)); if (true == bThereIsLogFile) { #ifdef CHART_IMPLEMENT Menu_EnableItem(g_hMenuChart,true); #endif Menu_EnableItem(g_hMenuOpenLogFile, true); } return 0; }
FILE *open_log_file(const char *log_name, const char *mode) { char *path = get_log_file(log_name); FILE *f = fopen(path, mode); if (!f) { fatal("could not open log file: %s", strerror(errno)); } free(path); return f; }
INT_PTR QuotesMenu_OpenLogFile(WPARAM wp, LPARAM) { MCONTACT hContact = MCONTACT(wp); if (NULL == hContact) return 0; tstring sLogFileName; if ((true == get_log_file(hContact, sLogFileName)) && (false == sLogFileName.empty())) ::ShellExecute(NULL, _T("open"), sLogFileName.c_str(), NULL, NULL, SW_SHOWNORMAL); return 0; }
void ensure_log_file_exists(const char *log_name) { ensure_log_dir_exists(); char *log_file = get_log_file(log_name); struct stat st; if (stat(log_file, &st) == -1 && errno != ENOENT) { fatal("couldn't stat log file: %s", strerror(errno)); } if (errno == ENOENT) { /* Log file doesn't exist, create it */ if (creat(log_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) { fatal("couldn't create log file: %s", strerror(errno)); } } else if (!S_ISREG(st.st_mode)) { fatal("log file path already exists, but isn't a regular file"); } free(log_file); }
int main(int argc, char *argv[]) { int files; /* number of files in each dir */ int totfiles = 0; int dirs; /* directories in each dir */ int totdirs = 0; int levels; /* levels deep */ char *fname; char *dname; struct timeval time; char *opts; struct testparam *param; struct btest *b; char *config_file; char *test_dir; char *log_file; FILE *log; setbuf(stdout, NULL); Myname = *argv++; argc--; while(argc && **argv == '-') { for(opts = &argv[0][1]; *opts; opts++) { switch (*opts) { case 'h': /* help */ usage(); exit(1); break; case 's': /* silent */ Sflag++; break; case 't': /* time */ Tflag++; break; case 'f': /* funtionality */ Fflag++; break; case 'n': /* No Test Directory create */ Nflag++; break; default: error("unknown option '%c'", *opts); usage(); exit(1); } } argc--; argv++; } if(argc) { config_file = *argv; argc--; argv++; } else { fprintf(stderr, "Missing config_file"); exit(1); } if(argc != 0) { fprintf(stderr, "too many parameters"); usage(); exit(1); } param = readin_config(config_file); if(param == NULL) { fprintf(stderr, "Nothing built\n"); exit(1); } b = get_btest_args(param, ONE); if(b == NULL) { fprintf(stderr, "Missing basic test number 1 in the config file '%s'\n", config_file); free_testparam(param); exit(1); } if(b->levels == -1) { fprintf(stderr, "Missing 'levels' parameter in the config file '%s' for the basic test number 1\n", config_file); free_testparam(param); exit(1); } if(b->files == -1) { fprintf(stderr, "Missing 'files' parameter in the config file '%s' for the basic test number 1\n", config_file); free_testparam(param); exit(1); } if(b->dirs == -1) { fprintf(stderr, "Missing 'dirs' parameter in the config file '%s' for the basic test number 1\n", config_file); free_testparam(param); exit(1); } levels = b->levels; files = b->files; dirs = b->dirs; fname = b->fname; dname = b->dname; test_dir = get_test_directory(param); log_file = get_log_file(param); free_testparam(param); if(!Fflag) { Tflag = 0; levels = 2; files = 2; dirs = 2; } if(!Sflag) { fprintf(stdout, "%s: File and directory creation test\n", Myname); } if(!Nflag) testdir(test_dir); else mtestdir(test_dir); starttime(); dirtree(levels, files, dirs, fname, dname, &totfiles, &totdirs); endtime(&time); if(!Sflag) { fprintf(stdout, "\tcreated %d files %d directories %d levels deep", totfiles, totdirs, levels); } if(Tflag && !Sflag) { fprintf(stdout, " in %ld.%02ld seconds", (long)time.tv_sec, (long)time.tv_usec / 10000); } if(!Sflag) { fprintf(stdout, "\n"); } if((log = fopen(log_file, "a")) == NULL) { printf("Enable to open the file '%s'\n", log_file); complete(); } fprintf(log, "b1\t%d\t%d\t%d\t%ld.%02ld\n", totfiles, totdirs, levels, (long)time.tv_sec, (long)time.tv_usec / 10000); fclose(log); complete(); }
int popc_logger_t(LOGLEVEL level, const char* file, int line, const char* function, const char* tag, const char* format, ...) { // Check if message level in higher than our threshold if (level < MIN_LOG_LEVEL) return 0; // Use file name without path to avoid having the full user path in logs const char* basename = strrchr(file, '/'); if (basename == nullptr) basename = file; else basename += 1; auto log_file = get_log_file(); // Time time_t rawtime; time(&rawtime); const tm* timeinfo = localtime(&rawtime); char dd[20]; strftime(dd, sizeof(dd), "%Y-%m-%d %H:%M:%S", timeinfo); char msg[512]; va_list ap; va_start(ap, format); vsnprintf(msg, sizeof(msg), format, ap); va_end(ap); auto msg_length = strlen(msg); if (msg[msg_length - 1] == '\n') { msg[msg_length - 1] = ' '; } // Print the message to stderr or stdout if (level >= MIN_STDERR_LEVEL) if (tag) { fprintf(stderr, "%s %5d %s [%5s] %s (%s:%d %s)\n", dd, getpid(), get_prefix(level), tag, msg, basename, line, function); } else { fprintf(stderr, "%s %5d %s %s (%s:%d %s)\n", dd, getpid(), get_prefix(level), msg, basename, line, function); } else if (level >= MIN_STDOUT_LEVEL) fprintf(stdout, "%s\n", msg); // Print the message to file FILE* f = fopen(log_file.c_str(), "a"); if (f == NULL) { fprintf(stderr, "ERROR: Impossible to open log file %s\n", log_file.c_str()); return 1; } if (tag) { fprintf(f, "%s %5d %s [%5s] %s (%s:%d %s)\n", dd, getpid(), get_prefix(level), tag, msg, basename, line, function); } else { fprintf(f, "%s %5d %s %s (%s:%d %s)\n", dd, getpid(), get_prefix(level), msg, basename, line, function); } fclose(f); return 0; }