Debian Codegraph @

Logger/Logger.cpp

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #include <iomanip>
22 #include <sstream>
23 #include <sys/time.h> // gettimeofday
24
25 #include "Logger/Logger.h"
26
27 using std::string;
28
29 namespace Logger
30 {
31 Logger::Level Logger::logLevel = Logger::LevelInfo;
32
33 string Logger::DateTime()
34 {
35 char buffer[27];
36 struct timeval timestamp;
37 gettimeofday(&timestamp, NULL);
38 struct tm tm;
39 gmtime_r(&timestamp.tv_sec, &tm);
40 strftime(buffer, sizeof(buffer), "%F %T.", &tm);
41 snprintf(buffer + 20, sizeof(buffer) - 20, "%06li", static_cast<long>(timestamp.tv_usec));
42 return string(buffer);
43 }
44
45 void Logger::Replace(string& workString,
46 const unsigned char argument,
47 const std::string& value)
48 {
49 std::string needle = "{" + std::to_string(argument) + "}";
50 size_t pos = workString.find(needle);
51 if (pos == std::string::npos)
52 {
53 return;
54 }
55 workString.replace(pos, needle.size(), value);
56 }
57
58 void Logger::AsciiPart(std::stringstream& output, const unsigned char* input, const size_t size)
59 {
60 output << " ";
61 if (size < 8)
62 {
63 output << " ";
64 }
65 for (size_t index = size; index < 16; ++index)
66 {
67 output << " ";
68 }
69 for (size_t index = 0; index < size; ++index)
70 {
71 if (index == 8)
72 {
73 output << " ";
74 }
75 if (input[index] >= 0x20 && input[index] < 127)
76 {
77 output << input[index];
78 }
79 else
80 {
81 output << ".";
82 }
83 }
84 }
85
86 void Logger::Hex(const unsigned char direction, const unsigned char* input, const size_t size)
87 {
88 std::stringstream output;
89 size_t index;
90 for (index = 0; index < size; ++index)
91 {
92 if ((index & 0x0F) == 0)
93 {
94 if (direction == 1)
95 {
96 output << "<< ";
97 }
98 else if (direction == 2)
99 {
100 output << " >> ";
101 }
102 output << "0x" << std::setfill('0') << std::setw(4) << std::hex << index << " ";
103 }
104
105 output << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(input[index]) << " ";
106
107 size_t next = index + 1;
108 if ((next & 0x0F) == 0)
109 {
110 AsciiPart(output, input + index - 15, 16);
111 Debug(output.str());
112 output.str(std::string());
113 if (next == size)
114 {
115 return;
116 }
117 continue;
118 }
119 if ((next & 0x07) == 0)
120 {
121 output << " ";
122 }
123 }
124 size_t reminder = (index & 0x0F);
125 AsciiPart(output, input + index - reminder, reminder);
126 Debug(output.str());
127 }
128 }
129