void ConfigureLogger() { #if defined(_WIN32) TCHAR path_buffer[MAX_PATH]; DWORD buffer_size = GetModuleFileName(NULL, path_buffer, MAX_PATH); // if buffer_size == MAX_PATH the buffer was truncated and GetLastError() returns ERROR_INSUFFICIENT_BUFFER // TODO: if buffer_size == 0 check GetLastError() boost::filesystem::path log_path(path_buffer); #elif defined(__APPLE__) char path_buffer[MAXPATHLEN]; std::uint32_t buffer_size = sizeof(path_buffer); int got_path = _NSGetExecutablePath(path_buffer, &buffer_size); // TODO: check if got_path != 0, in which case the call failed. boost::filesystem::path log_path(path_buffer); #else // Assuming linux # if !defined(NDEBUG) // On debug the configuration file is in the same directory as the executable char path_buffer[8192]; std::uint32_t buffer_size = sizeof(path_buffer); ssize_t path_size = readlink("/proc/self/exe", path_buffer, buffer_size); // TODO: if path_size == -1 check errno path_buffer[path_size] = '\0'; boost::filesystem::path log_path(path_buffer); # else // On release mode, it should be in a common directory boost::filesystem::path log_path("/var/log/actor/logging.conf"); # endif #endif log_path.remove_filename(); log_path /= "logging.conf"; // only in version 1.1.1 and beyond // log4cplus::initialize(); if (boost::filesystem::exists(log_path) && boost::filesystem::is_regular_file(log_path)) { log4cplus::PropertyConfigurator configurator(log_path.string()); configurator.configure(); } else { log4cplus::BasicConfigurator configurator; configurator.configure(); // TODO: make this code work /*log4cplus::SharedAppenderPtr appender( new log4cplus::ConsoleAppender(false, false)); appender->setName("RootAppender"); std::auto_ptr<log4cplus::Layout> layout( new log4cplus::PatternLayout("%D{%d.%m.%Y %H:%M:%S.%q} [%8t] %-5p - " "%c{2} - %m%n")); appender->setLayout(layout); auto logger = log4cplus::Logger::getRoot(); logger.addAppender(appender); logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);*/ } }
void do_common_options(const QOptions &options, const QString& appName) { if (options.value(QString::fromLatin1("help")).toBool()) { options.print(); exit(0); } // has no effect if qInstallMessageHandler() called //qSetMessagePattern("%{function} @%{line}: %{message}"); #if !defined(Q_OS_WINRT) && !defined(Q_OS_ANDROID) QString app(appName); if (app.isEmpty() && qApp) app = qApp->applicationName(); QString logfile(options.option(QString::fromLatin1("logfile")).value().toString().arg(app)); if (!logfile.isEmpty()) { if (QDir(logfile).isRelative()) { QString log_path(QString::fromLatin1("%1/%2").arg(qApp->applicationDirPath()).arg(logfile)); QFile f(log_path); if (!f.open(QIODevice::WriteOnly)) { log_path = QString::fromLatin1("%1/%2").arg(appDataDir()).arg(logfile); qDebug() << "executable dir is not writable. log to " << log_path; } logfile = log_path; } qDebug() << "set log file: " << logfile; fileLogger()->setFileName(logfile); if (fileLogger()->open(QIODevice::WriteOnly)) { qDebug() << "Logger"; qInstallMessageHandler(Logger); } else { qWarning() << "Failed to open log file '" << fileLogger()->fileName() << "': " << fileLogger()->errorString(); } } #endif QByteArray level(options.value(QString::fromLatin1("log")).toByteArray()); if (level.isEmpty()) level = Config::instance().logLevel().toLatin1(); if (!level.isEmpty()) qputenv("QTAV_LOG", level); }
int main( int argc, char *argv[]) { /* Array for the log entries for the specified job */ FILE *fp; int i, j; int file_count; char *filenames[MAX_LOG_FILES_PER_DAY]; /* full path of logfiles to read */ struct tm *tm_ptr; time_t t, t_save; signed char c; char *prefix_path = NULL; int number_of_days = 1; char *endp; short error = 0; int opt; char no_acct = 0, no_svr = 0, no_mom = 0, no_schd = 0; char verbosity = 1; int wrap = -1; int log_filter = 0; int event_type; char filter_excessive = 0; int excessive_count; #if defined(FILTER_EXCESSIVE) filter_excessive = 1; #endif #if defined(EXCESSIVE_COUNT) excessive_count = EXCESSIVE_COUNT; #endif while ((c = getopt(argc, argv, "qzvamslw:p:n:f:c:")) != EOF) { switch (c) { case 'q': verbosity = 0; break; case 'v': verbosity = 2; break; case 'a': no_acct = 1; break; case 's': no_svr = 1; break; case 'm': no_mom = 1; break; case 'l': no_schd = 1; break; case 'z': filter_excessive = filter_excessive ? 0 : 1; break; case 'c': excessive_count = strtol(optarg, &endp, 10); if (*endp != '\0') error = 1; break; case 'w': wrap = strtol(optarg, &endp, 10); if (*endp != '\0') error = 1; break; case 'p': prefix_path = optarg; break; case 'n': number_of_days = strtol(optarg, &endp, 10); if (*endp != '\0') error = 1; break; case 'f': if (!strcmp(optarg, "error")) log_filter |= PBSEVENT_ERROR; else if (!strcmp(optarg, "system")) log_filter |= PBSEVENT_SYSTEM; else if (!strcmp(optarg, "admin")) log_filter |= PBSEVENT_ADMIN; else if (!strcmp(optarg, "job")) log_filter |= PBSEVENT_JOB; else if (!strcmp(optarg, "job_usage")) log_filter |= PBSEVENT_JOB_USAGE; else if (!strcmp(optarg, "security")) log_filter |= PBSEVENT_SECURITY; else if (!strcmp(optarg, "sched")) log_filter |= PBSEVENT_SCHED; else if (!strcmp(optarg, "debug")) log_filter |= PBSEVENT_DEBUG; else if (!strcmp(optarg, "debug2")) log_filter |= PBSEVENT_DEBUG2; else if (isdigit(optarg[0])) { log_filter = strtol(optarg, &endp, 16); if (*endp != '\0') error = 1; } else error = 1; break; default: error = 1; break; } } /* END while ((c = getopt(argc,argv,"zvamslw:p:n:f:c:")) != EOF) */ /* no jobs */ if ((error != 0) || (argc == optind)) { printf("USAGE: %s [-a|s|l|m|q|v|z] [-c count] [-w size] [-p path] [-n days] [-f filter_type] <JOBID>\n", strip_path(argv[0])); printf( " -p : path to PBS_SERVER_HOME\n" " -w : number of columns of your terminal\n" " -n : number of days in the past to look for job(s) [default 1]\n" " -f : filter out types of log entries, multiple -f's can be specified\n" " error, system, admin, job, job_usage, security, sched, debug, \n" " debug2, or absolute numeric hex equivalent\n" " -z : toggle filtering excessive messages\n"); printf( " -c : what message count is considered excessive\n" " -a : don't use accounting log files\n" " -s : don't use server log files\n" " -l : don't use scheduler log files\n" " -m : don't use mom log files\n" " -q : quiet mode - hide all error messages\n" " -v : verbose mode - show more error messages\n"); printf("default prefix path = %s\n", PBS_SERVER_HOME); #if defined(FILTER_EXCESSIVE) printf("filter_excessive: ON\n"); #else printf("filter_excessive: OFF\n"); #endif return(1); } /* END if ((error != 0) || (argc == optind)) */ if (wrap == -1) wrap = get_cols(); time(&t); t_save = t; for (opt = optind;opt < argc;opt++) { for (i = 0, t = t_save;i < number_of_days;i++, t -= SECONDS_IN_DAY) { tm_ptr = localtime(&t); for (j = 0;j < 4;j++) { if ((j == IND_ACCT && no_acct) || (j == IND_SERVER && no_svr) || (j == IND_MOM && no_mom) || (j == IND_SCHED && no_schd)) continue; file_count = log_path(prefix_path, j, tm_ptr, filenames); /* there can be multiple server and mom log files per day */ /* traverse filenames until we have got them all */ if (file_count < 0) { printf("Error getting file names\n"); continue; } for (; file_count > 0; file_count--) { if ((fp = fopen(filenames[file_count-1], "r")) == NULL) { if (verbosity >= 1) perror(filenames[file_count-1]); continue; } if (parse_log(fp, argv[opt], j) < 0) { /* no valid entries located in file */ if (verbosity >= 1) { fprintf(stderr, "%s: No matching job records located\n", filenames[file_count-1]); } } else if (verbosity >= 2) { fprintf(stderr, "%s: Successfully located matching job records\n", filenames[file_count-1]); } fclose(fp); free(filenames[file_count-1]); } /* end of for file_count */ } } /* END for (i) */ if (filter_excessive) filter_excess(excessive_count); qsort(log_lines, ll_cur_amm, sizeof(struct log_entry), sort_by_date); if (ll_cur_amm != 0) { printf("\nJob: %s\n\n", log_lines[0].name); } for (i = 0;i < ll_cur_amm;i++) { if (log_lines[i].log_file == 'A') event_type = 0; else event_type = strtol(log_lines[i].event, &endp, 16); if (!(log_filter & event_type) && !(log_lines[i].no_print)) { printf("%-20s %-5c", log_lines[i].date, log_lines[i].log_file); line_wrap(log_lines[i].msg, 26, wrap); } } } /* END for (opt) */ return(0); } /* END main() */
static void construct_file_queues ( const char *status_dir, const char *dir ) { DIR *dirp; struct dirent *dp; dirp = opendir (dir); if ( dirp == NULL ) return; struct stat sb; for ( ; ; ) { dp = readdir (dirp); if ( dp == NULL ) { break; } if ( strcmp (dp->d_name, ".") == 0 || strcmp (dp->d_name, "..") == 0 ) { continue; } std::string path (dir); path.append (dp->d_name); lstat (path.c_str (), &sb); if ( ! S_ISDIR (sb.st_mode) ) { continue; } std::string log_path (dir); log_path.append (dp->d_name); std::string status_path (status_dir); status_path.append (dp->d_name); hosts.push_back (dp->d_name); log_dirs.push_back (log_path); mkdir (status_path.c_str (), S_IRWXU | S_IRWXG | S_IRWXO); status_dirs.push_back (status_path); std::string status_total_file (status_path); status_total_file.append ("/total_status.log"); int fd = open (status_total_file.c_str (), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); fstat (fd, &sb); int status_file_size = sb.st_size; if ( status_file_size == 0 ) { const char *buf = "0000000000000000"; write (fd, ( void * ) buf, 2 * sizeof (uint64_t )); } char *addr = ( char * ) mmap (NULL, 2 * sizeof (uint64_t ), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if ( addr == MAP_FAILED ) { return; } if ( status_file_size == 0 ) { *( uint64_t * ) addr = 0; *( uint64_t * ) ( addr + sizeof (uint64_t ) ) = 0; } else { uint64_t has_sync = * ( uint64_t * ) ( addr + sizeof (uint64_t ) ); *( uint64_t * ) addr = has_sync; } close (fd); total_status_addrs.push_back (addr); std::deque<File *> files; file_queue.push_back (files); int *pipe_fd = ( int * ) calloc (2, sizeof (int )); pipe (pipe_fd); pipe_fds.push_back (pipe_fd); } closedir (dirp); int size = status_dirs.size (); for ( int i = 0; i < size; i ++ ) { construct_file_queue (i, i); } }