File: Logger.cpp

package info (click to toggle)
railcontrol 24%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,680 kB
  • sloc: cpp: 40,834; javascript: 2,509; makefile: 144; php: 97; sh: 60
file content (129 lines) | stat: -rw-r--r-- 3,018 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
RailControl - Model Railway Control Software

Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org

RailControl is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.

RailControl is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RailControl; see the file LICENCE. If not see
<http://www.gnu.org/licenses/>.
*/

#include <iomanip>
#include <sstream>
#include <sys/time.h> // gettimeofday

#include "Logger/Logger.h"

using std::string;

namespace Logger
{
	Logger::Level Logger::logLevel = Logger::LevelInfo;

	string Logger::DateTime()
	{
		char buffer[27];
		struct timeval timestamp;
		gettimeofday(&timestamp, NULL);
		struct tm tm;
		gmtime_r(&timestamp.tv_sec, &tm);
		strftime(buffer, sizeof(buffer), "%F %T.", &tm);
		snprintf(buffer + 20, sizeof(buffer) - 20, "%06li", static_cast<long>(timestamp.tv_usec));
		return string(buffer);
	}

	void Logger::Replace(string& workString,
		const unsigned char argument,
		const std::string& value)
	{
		std::string needle = "{" + std::to_string(argument) + "}";
		size_t pos = workString.find(needle);
		if (pos == std::string::npos)
		{
			return;
		}
		workString.replace(pos, needle.size(), value);
	}

	void Logger::AsciiPart(std::stringstream& output, const unsigned char* input, const size_t size)
	{
		output << "  ";
		if (size < 8)
		{
			output << "  ";
		}
		for (size_t index = size; index < 16; ++index)
		{
			output << "   ";
		}
		for (size_t index = 0; index < size; ++index)
		{
			if (index == 8)
			{
				output << " ";
			}
			if (input[index] >= 0x20 && input[index] < 127)
			{
				output << input[index];
			}
			else
			{
				output << ".";
			}
		}
	}

	void Logger::Hex(const unsigned char direction, const unsigned char* input, const size_t size)
	{
		std::stringstream output;
		size_t index;
		for (index = 0; index < size; ++index)
		{
			if ((index & 0x0F) == 0)
			{
				if (direction == 1)
				{
					output << "<<   ";
				}
				else if (direction == 2)
				{
					output << "  >> ";
				}
				output << "0x" << std::setfill('0') << std::setw(4) << std::hex << index << "   ";
			}

			output << std::setfill('0') << std::setw(2) << std::hex << static_cast<unsigned int>(input[index]) << " ";

			size_t next = index + 1;
			if ((next & 0x0F) == 0)
			{
				AsciiPart(output, input + index - 15, 16);
				Debug(output.str());
				output.str(std::string());
				if (next == size)
				{
					return;
				}
				continue;
			}
			if ((next & 0x07) == 0)
			{
				output << "  ";
			}
		}
		size_t reminder = (index & 0x0F);
		AsciiPart(output, input + index - reminder, reminder);
		Debug(output.str());
	}
}