formatter.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 // Heavily inspired by spdlog, Copyright(c) 2015 Gabi Melman
20 
21 #include <momemta/impl/logger/formatter.h>
22 
23 #include <momemta/impl/logger/os.h>
24 
25 namespace logger {
26 
27 void full_formatter::format(details::log_msg& msg) {
28  auto tm_time = details::os::localtime(log_clock::to_time_t(msg.time));
29 
30  auto duration = msg.time.time_since_epoch();
31  auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() % 1000;
32 
33  msg.formatted << msg.index << ": [" << static_cast<unsigned int>(tm_time.tm_year + 1900) << '-'
34  << std::setfill('0')
35  << std::setw(2) << static_cast<unsigned int>(tm_time.tm_mon + 1) << '-'
36  << std::setw(2) << static_cast<unsigned int>(tm_time.tm_mday) << ' '
37  << std::setw(2) << static_cast<unsigned int>(tm_time.tm_hour) << ':'
38  << std::setw(2) << static_cast<unsigned int>(tm_time.tm_min) << ':'
39  << std::setw(2) << static_cast<unsigned int>(tm_time.tm_sec) << '.'
40  << std::setw(3) << static_cast<unsigned int>(millis) << "] ";
41 
42  msg.formatted << '[';
43  format_level(msg);
44  msg.formatted << "] ";
45 
46  msg.formatted << msg.raw.str();
47 
48  msg.formatted.write(details::os::eol, details::os::eol_size);
49 }
50 
51 void full_formatter::format_level(details::log_msg& msg) {
52  msg.formatted << ::logging::level::to_str(msg.level);
53 }
54 
55 void ansi_color_full_formatter::format_level(details::log_msg& msg) {
56  switch (msg.level) {
57  case logging::level::trace:
58  case logging::level::debug:
59  msg.formatted << grey;
60  break;
61  case logging::level::info:
62  msg.formatted << blue;
63  break;
64  case logging::level::warning:
65  msg.formatted << yellow << bold;
66  break;
67  case logging::level::error:
68  msg.formatted << red << bold;
69  break;
70  case logging::level::fatal:
71  msg.formatted << bold << on_red;
72  break;
73  case logging::level::off:
74  msg.formatted << reset;
75  default:
76  break;
77  }
78 
79  msg.formatted << ::logging::level::to_str(msg.level) << reset;
80 }
81 
82 }