* @threadsafety Always. */
| 470 | * @threadsafety Always. |
| 471 | */ |
| 472 | void CompatLogger::ReopenFile(bool rotate) |
| 473 | { |
| 474 | ObjectLock olock(this); |
| 475 | |
| 476 | String tempFile = GetLogDir() + "/icinga.log"; |
| 477 | |
| 478 | if (m_OutputFile) { |
| 479 | m_OutputFile.close(); |
| 480 | |
| 481 | if (rotate) { |
| 482 | String archiveFile = GetLogDir() + "/archives/icinga-" + Utility::FormatDateTime("%m-%d-%Y-%H", Utility::GetTime()) + ".log"; |
| 483 | |
| 484 | Log(LogNotice, "CompatLogger") |
| 485 | << "Rotating compat log file '" << tempFile << "' -> '" << archiveFile << "'"; |
| 486 | |
| 487 | (void) rename(tempFile.CStr(), archiveFile.CStr()); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | m_OutputFile.open(tempFile.CStr(), std::ofstream::app); |
| 492 | |
| 493 | if (!m_OutputFile) { |
| 494 | Log(LogWarning, "CompatLogger") |
| 495 | << "Could not open compat log file '" << tempFile << "' for writing. Log output will be lost."; |
| 496 | |
| 497 | return; |
| 498 | } |
| 499 | |
| 500 | WriteLine("LOG ROTATION: " + GetRotationMethod()); |
| 501 | WriteLine("LOG VERSION: 2.0"); |
| 502 | |
| 503 | for (const Host::Ptr& host : ConfigType::GetObjectsByType<Host>()) { |
| 504 | String output; |
| 505 | CheckResult::Ptr cr = host->GetLastCheckResult(); |
| 506 | |
| 507 | if (cr) |
| 508 | output = CompatUtility::GetCheckResultOutput(cr); |
| 509 | |
| 510 | std::ostringstream msgbuf; |
| 511 | msgbuf << "CURRENT HOST STATE: " |
| 512 | << host->GetName() << ";" |
| 513 | << GetHostStateString(host) << ";" |
| 514 | << Checkable::StateTypeToString(host->GetStateType()) << ";" |
| 515 | << host->GetCheckAttempt() << ";" |
| 516 | << output << ""; |
| 517 | |
| 518 | WriteLine(msgbuf.str()); |
| 519 | } |
| 520 | |
| 521 | for (const Service::Ptr& service : ConfigType::GetObjectsByType<Service>()) { |
| 522 | Host::Ptr host = service->GetHost(); |
| 523 | |
| 524 | String output; |
| 525 | CheckResult::Ptr cr = service->GetLastCheckResult(); |
| 526 | |
| 527 | if (cr) |
| 528 | output = CompatUtility::GetCheckResultOutput(cr); |
| 529 |