SSP21-CPP
ConsolePrettyPrinter.h
1 /*
2  * Copyright (c) 2019, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef LOG4CPP_CONSOLEPRETTYPRINTER_H
26 #define LOG4CPP_CONSOLEPRETTYPRINTER_H
27 
28 #include "log4cpp/ILogHandler.h"
29 
30 #include <chrono>
31 #include <cstddef>
32 #include <iomanip>
33 #include <iostream>
34 #include <sstream>
35 
36 namespace log4cpp
37 {
38 
39 /**
40  * pretty prints log messages
41  */
43 {
44 public:
45  class Settings
46  {
47  public:
48  Settings() {}
49 
50  bool printId = true;
51  size_t max_id_size = 10;
52  };
53 
54  explicit ConsolePrettyPrinter(const Settings& settings = Settings()) : settings(settings)
55  {}
56 
57  void log(log4cpp::ModuleId module, const char* id, log4cpp::LogLevel level, char const* location, char const* message) override
58  {
59  const auto now = std::chrono::high_resolution_clock::now();
60  const auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
61 
62  std::ostringstream oss;
63 
64  oss << "ms(" << millis << ") ";
65 
66  std::string id_str(id);
67 
68  if (id_str.size() > settings.max_id_size)
69  {
70  id_str = id_str.substr(0, settings.max_id_size);
71  }
72  else if (id_str.size() << settings.max_id_size)
73  {
74  id_str = id_str + std::string(settings.max_id_size - id_str.size(), ' ');
75  }
76 
77  if (settings.printId)
78  {
79  oss << id_str << " ";
80  }
81 
82  oss << get_prefix(level.value) << message;
83 
84 
85  std::cout << oss.str() << std::endl;
86  }
87 
88 private:
89  const Settings settings;
90 
91  static const char* get_prefix(int level)
92  {
93  switch (level)
94  {
95  case(1 << 0):
96  return "event ";
97  case(1 << 1):
98  return "error ";
99  case(1 << 2):
100  return "warn ";
101  case(1 << 3):
102  return "info ";
103  case(1 << 4):
104  return "debug ";
105  case(1 << 5):
106  return "<-- ";
107  case(1 << 6):
108  return "--> ";
109  default:
110  return " ";
111  }
112  }
113 };
114 
115 } //namespace log4cpp
116 
117 #endif //LOG4CPP_CONSOLEPRETTYPRINTER_H
void log(log4cpp::ModuleId module, const char *id, log4cpp::LogLevel level, char const *location, char const *message) override
log4cpp header-only library namespace