Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Logging.cpp
Go to the documentation of this file.
1 //===-- serialbox/core/Logging.cpp --------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
13 //===------------------------------------------------------------------------------------------===//
14 
15 #include "serialbox/core/Logging.h"
16 #include <boost/format.hpp>
17 #include <chrono>
18 
19 namespace serialbox {
20 
21 namespace internal {
22 
24 static std::string getCurrentTime() {
25  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
26  auto now_ms = now.time_since_epoch();
27  auto now_sec = std::chrono::duration_cast<std::chrono::seconds>(now_ms);
28  auto tm_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now_ms - now_sec);
29 
30  std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
31  struct tm* localTime = std::localtime(&currentTime);
32 
33  return (boost::format("%02i:%02i:%02i.%03i") % localTime->tm_hour % localTime->tm_min %
34  localTime->tm_sec % tm_ms.count())
35  .str();
36 }
37 
38 NullLogger* NullLogger::instance_ = nullptr;
39 
40 NullLogger& NullLogger::getInstance() noexcept {
41  if(!instance_)
42  instance_ = new NullLogger();
43  return (*instance_);
44 }
45 
46 Logger* Logger::instance_ = nullptr;
47 
48 Logger& Logger::getInstance() noexcept {
49  if(!instance_)
50  instance_ = new Logger();
52  return (*instance_);
53 }
54 
55 LoggerProxy::~LoggerProxy() { std::cout << std::endl; }
56 
57 LoggerProxy Logger::trace() noexcept {
58  std::cout << "[" << getCurrentTime() << "] [trace] ";
59  return LoggerProxy();
60 }
61 
62 LoggerProxy Logger::debug() noexcept {
63  std::cout << "[" << getCurrentTime() << "] [debug] ";
64  return LoggerProxy();
65 }
66 
67 LoggerProxy Logger::info() noexcept {
68  std::cout << "[" << getCurrentTime() << "] [info] ";
69  return LoggerProxy();
70 }
71 
72 LoggerProxy Logger::warning() noexcept {
73  std::cout << "[" << getCurrentTime() << "] [warning] ";
74  return LoggerProxy();
75 }
76 
77 LoggerProxy Logger::error() noexcept {
78  std::cout << "[" << getCurrentTime() << "] [error] ";
79  return LoggerProxy();
80 }
81 
82 LoggerProxy Logger::fatal() noexcept {
83  std::cout << "[" << getCurrentTime() << "] [fatal] ";
84  return LoggerProxy();
85 }
86 
87 bool LoggingIsEnabled = false;
88 
89 } // namespace internal
90 
91 } // namespace serialbox
static void enable() noexcept
Disable logging.
Definition: Logging.h:81
Namespace of the serialbox library.
Definition: Archive.h:25