// Copyright 2013 Google Inc. All Rights Reserved. // // Log - implemented using stdout. #include #include #include #include #include "log.h" namespace { FILE* output_file = stdout; } // namespace namespace wvcdm { LogPriority g_cutoff = LOG_WARN; void InitLogging(int argc, const char* const* argv) { // default to stdout if no arguments set it otherwise. output_file = stdout; for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "-v")) { g_cutoff = LOG_VERBOSE; } else if (!strcmp(argv[i], "-")) { // default above is stdout, so keep that. } else { // open a file for output. output_file = fopen(argv[i], "a"); if (!output_file) { output_file = stdout; printf("Could not open log file \"%s\"\n", argv[i]); } } } const char *verbose_env = getenv("VERBOSE_LOG"); if (verbose_env && ! strncmp(verbose_env, "yes", 3) ) { g_cutoff = LOG_VERBOSE; } } void Log(const char* file, int line, LogPriority level, const char* fmt, ...) { const char* severities[] = { "ERROR", "WARN", "INFO", "DEBUG", "VERBOSE" }; if (level >= sizeof(severities) / sizeof(*severities)) { fprintf(output_file, "[FATAL:%s(%d)] Invalid log priority level: %d\n", file, line, level); return; } if (level > g_cutoff) return; fprintf(output_file, "[%s:%s(%d)] ", severities[level], file, line); va_list ap; va_start(ap, fmt); vfprintf(output_file, fmt, ap); va_end(ap); putc('\n', output_file); fflush(output_file); } }; // namespace wvcdm