-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbasic_formatter.cpp
More file actions
79 lines (64 loc) · 1.91 KB
/
Copy pathbasic_formatter.cpp
File metadata and controls
79 lines (64 loc) · 1.91 KB
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
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "log/basic_formatter.h"
#include <chrono>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "log/log_level.h"
namespace
{
/**
* Helper function to extract just the filename from a full path.
*
* @param filename
* Filename to extract from.
*
* @returns
* Filename from supplied string.
*/
std::string format_filename(const std::string &filename)
{
// find last occurrence of file separator
const auto index = filename.rfind(std::filesystem::path::preferred_separator);
return std::string{filename.substr(index + 1)};
}
/**
* Convert log level to string and get first character.
*
* @param level
* Log level to get first character of.
*
* @returns
* First character of log level.
*/
char first_char_of_level(const iris::LogLevel level)
{
std::stringstream strm{};
strm << level;
const auto str = strm.str();
return !str.empty() ? str.front() : 'U';
}
}
namespace iris
{
std::string BasicFormatter::format(
const LogLevel level,
const std::string &tag,
const std::string &message,
const std::string &filename,
const int line)
{
const auto now = std::chrono::system_clock::now();
const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
std::stringstream strm{};
strm << first_char_of_level(level) << " " << seconds.count() << " [" << tag << "] " << format_filename(filename)
<< ":" << line << " | " << message;
return strm.str();
}
}