void Log::initMe() { setDefaultConfig(); m_interceptMouseEvents = m_settings ? m_settings->value("Log/mouseEvents", false).toBool() : false; m_interceptQtMessages = m_settings ? m_settings->value("Log/messagesQt", true).toBool() : false; m_logRotateCounter = m_settings ? m_settings->value("Log/logRotateCounter", 5).toLongLong() : 5; QString logRotateFileSize = m_settings ? m_settings->value("Log/logRotateFileSize", "50mb").toString() : "50mb"; if (logRotateFileSize.contains("kb", Qt::CaseInsensitive)) m_logRotateFileSize = logRotateFileSize.remove("kb", Qt::CaseInsensitive).toInt() * 1024; else if (logRotateFileSize.contains("mb", Qt::CaseInsensitive)) m_logRotateFileSize = logRotateFileSize.remove("mb", Qt::CaseInsensitive).toInt() * 1024000; else m_logRotateFileSize = logRotateFileSize.toInt(); m_sync = m_settings ? m_settings->value("Log/synchronously", true).toBool() : true; m_enabled = m_settings ? m_settings->value("Log/enabled", true).toBool() : true; m_isInited = true; /// do logrotate if needed logRotate(); /// process it after exit from Core::init because gCore->scene() is not inited yet if (m_interceptMouseEvents) QTimer::singleShot(0, this, SLOT(initMouseHandler())); /// Setting up the qDebug handler only after m_isInited = true ensures that /// qDebug statements within the logger initialization don't cause stack (as /// warnings need to be logged if a log call is made during logger init.) if (m_interceptQtMessages) startCustomHandler(); if (m_enabled) { QString context_size = m_settings ? m_settings->value("Log/contextMaxSize", "1kb").toString() : "1kb"; int s = 0; if (context_size.contains("kb", Qt::CaseInsensitive)) s = context_size.remove("kb", Qt::CaseInsensitive).toInt() * 1024; else if (context_size.contains("mb", Qt::CaseInsensitive)) s = context_size.remove("mb", Qt::CaseInsensitive).toInt() * 1024000; else s = logRotateFileSize.toInt(); m_logProcessor = new LogProcessor(); m_logProcessor->setContextSize(s); m_logProcessor->setMouseEventFile(m_settings ? m_settings->value("Log/mouseLogFilename", "").toString() : ""); m_logProcessor->setFilters(this->getFilters()); m_logProcessor->setGeneralMatrix( this->getGeneralLevelsSettings()); m_logProcessor->setModulesMatrix( this->getModulesLevelsSettings()); /// synchronous or asynchronous /// if (m_sync) { log_stderr("Log is synchronous"); connect(this, SIGNAL(logMessage(CuteReport::LogLevel,QString,QString,QString)), m_logProcessor, SLOT(push(CuteReport::LogLevel,QString,QString,QString)), Qt::DirectConnection); } else { log_stderr("Log is asynchronous"); m_processorThread = new QThread(this); m_logProcessor->moveToThread(m_processorThread); connect(this, SIGNAL(logMessage(CuteReport::LogLevel,QString,QString,QString)), m_logProcessor, SLOT(push(CuteReport::LogLevel,QString,QString,QString)), Qt::QueuedConnection); m_processorThread->start(QThread::LowestPriority); log_stderr(QString("Log current thread pointer is %1").arg(qint64(this->thread()))); log_stderr(QString("Log processor thread pointer is %1").arg(qint64(m_logProcessor->thread()))); } } }
int apxLogWrite( HANDLE hFile, DWORD dwLevel, BOOL bTimeStamp, LPCSTR szFile, DWORD dwLine, LPCSTR szFormat, ...) { va_list args; CHAR buffer[1024+32] = ""; LPSTR szBp; int len = 0; LPCSTR f = szFile; CHAR sb[SIZ_PATHLEN]; DWORD wr; DWORD err; BOOL dolock = TRUE; apx_logfile_st *lf = (apx_logfile_st *)hFile; err = GetLastError(); /* save the last Error code */ if (IS_INVALID_HANDLE(lf)) lf = _st_sys_loghandle; if (IS_INVALID_HANDLE(lf)) { lf = &_st_sys_errhandle; lf->hFile = GetStdHandle(STD_ERROR_HANDLE); dolock = FALSE; } if (dwLevel < lf->dwLogLevel) return 0; if (f && (lf->dwLogLevel == APXLOG_LEVEL_DEBUG || dwLevel == APXLOG_LEVEL_ERROR)) { f = (szFile + lstrlenA(szFile) - 1); while(f != szFile && '\\' != *f && '/' != *f) f--; if(f != szFile) f++; } else f = NULL; szBp = buffer; if (!szFormat) { if (err == 0) { lstrcpyA(szBp, "Unknown error code"); if (dwLevel == APXLOG_LEVEL_ERROR) { szBp += 18; wsprintfA(szBp, " occured in (%s:%d) ", f, dwLine); } } else FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), szBp, 1000, NULL); } else { va_start(args, szFormat); wvsprintfA(szBp, szFormat, args); va_end(args); } len = lstrlenA(buffer); if (len > 0) { /* Remove trailing line separator */ if (buffer[len - 1] == '\n') buffer[--len] = '\0'; if (len > 0 && buffer[len - 1] == '\r') buffer[--len] = '\0'; if (!IS_INVALID_HANDLE(lf->hFile)) { SYSTEMTIME t; GetLocalTime(&t); if (dolock) { APX_LOGLOCK(lf->hFile); logRotate(lf, &t); } if (bTimeStamp) { wsprintfA(sb, "[%d-%02d-%02d %02d:%02d:%02d] ", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond); WriteFile(lf->hFile, sb, lstrlenA(sb), &wr, NULL); } WriteFile(lf->hFile, _log_level[dwLevel], lstrlenA(_log_level[dwLevel]), &wr, NULL); if (f && lf->dwLogLevel == APXLOG_LEVEL_DEBUG) { wsprintfA(sb, "(%10s:%-4d) ", f, dwLine); WriteFile(lf->hFile, sb, lstrlenA(sb), &wr, NULL); } if (len) WriteFile(lf->hFile, buffer, len, &wr, NULL); /* Terminate the line */ WriteFile(lf->hFile, LINE_SEP, sizeof(LINE_SEP) - 1, &wr, NULL); #ifdef _DEBUG_FULL FlushFileBuffers(lf->hFile); #endif if (dolock) { APX_LOGUNLOCK(lf->hFile); } } #ifdef _DEBUG_FULL { char tid[1024 + 16]; wsprintfA(tid, "[%04d] %s", GetCurrentThreadId(), buffer); OutputDebugStringA(tid); } #endif } /* Restore the last Error code */ SetLastError(err); if (szFormat && err != 0 && dwLevel == APXLOG_LEVEL_ERROR) { /* Print the System error description */ apxLogWrite(hFile, dwLevel, bTimeStamp, szFile, dwLine, NULL); } return len; }
/* sleep until eval or retry RealTime */ void sleepTight(Task *t, int type) { RealTime sched; RealTime delay; /* interval to sleep */ int sts; RealTime cur_entry = getReal(); #ifdef HAVE_WAITPID pid_t pid; if (need_wait) { /* harvest terminated children */ while ((pid = waitpid(-1, &sts, WNOHANG)) > (pid_t)0) { #if PCP_DEBUG if (pmDebug & DBG_TRACE_APPL2) { fprintf(stderr, "sleepTight: wait: pid=%" FMT_PID " done status=0x%x", pid, sts); if (WIFEXITED(sts)) fprintf(stderr, " exit=%d", WEXITSTATUS(sts)); if (WIFSIGNALED(sts)) fprintf(stderr, " signal=%d", WTERMSIG(sts)); fprintf(stderr, "\n"); } #endif ; } need_wait = 0; } #endif if (!archives) { struct timespec ts, tleft; static RealTime last_sched = -1; static Task *last_t; static int last_type; RealTime cur = getReal(); sched = type == SLEEP_EVAL ? t->eval : t->retry; delay = sched - cur; if (delay < 0) { int show_detail = 0; if (delay <= -1) { fprintf(stderr, "sleepTight: negative delay (%f). sched=%f, cur=%f\n", delay, sched, cur); show_detail = 1; } #if PCP_DEBUG else { if (pmDebug & DBG_TRACE_APPL2) { fprintf(stderr, "sleepTight: small negative delay (%f). sched=%f, cur=%f\n", delay, sched, cur); show_detail = 1; } } #endif if (show_detail) { if (last_sched > 0) { fprintf(stderr, "Last sleepTight (%s) until: ", last_type == SLEEP_EVAL ? "eval" : "retry"); showFullTime(stderr, last_sched); fputc('\n', stderr); fprintf(stderr, "Last "); dumpTask(last_t); } fprintf(stderr, "This sleepTight() entry: "); showFullTime(stderr, cur_entry); fputc('\n', stderr); fprintf(stderr, "Harvest children done: "); showFullTime(stderr, cur); fputc('\n', stderr); fprintf(stderr, "Want sleepTight (%s) until: ", type == SLEEP_EVAL ? "eval" : "retry"); showFullTime(stderr, sched); fputc('\n', stderr); fprintf(stderr, "This "); dumpTask(t); } } else { unrealizenano(delay, &ts); for (;;) { /* loop to catch early wakeup from nanosleep */ if (ts.tv_sec < 0 || ts.tv_nsec > 999999999) { fprintf(stderr, "sleepTight: invalid args: %ld %ld\n", (long int)ts.tv_sec, (long int)ts.tv_nsec); break; } sts = nanosleep(&ts, &tleft); /* deferred signal handling done immediately */ if (doexit) exit(doexit); if (dorotate) { logRotate(); dorotate = 0; } if (sts == 0 || (sts < 0 && oserror() != EINTR)) break; ts = tleft; } } last_t = t; last_type = type; last_sched = sched; } }