Skip to content

Commit 395cc6e

Browse files
Add timestamps for logs (#6375)
1 parent 902bfc7 commit 395cc6e

File tree

237 files changed

+113359
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+113359
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- NodeJS:
1010
- FIXED: Support `skip_waypoints` in Node bindings [#6060](https://github.com/Project-OSRM/osrm-backend/pull/6060)
1111
- Misc:
12+
- ADDED: Add timestamps for logs. [#6375](https://github.com/Project-OSRM/osrm-backend/pull/6375)
1213
- CHANGED: Improve performance of map matching via getPathDistance optimization. [#6378](https://github.com/Project-OSRM/osrm-backend/pull/6378)
1314
- CHANGED: Optimize RestrictionParser performance. [#6344](https://github.com/Project-OSRM/osrm-backend/pull/6344)
1415
- ADDED: Support floats for speed value in traffic updates CSV. [#6327](https://github.com/Project-OSRM/osrm-backend/pull/6327)

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ add_subdirectory(${FLATBUFFERS_SRC_DIR}
456456
${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-build
457457
EXCLUDE_FROM_ALL)
458458

459+
set(FMT_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/fmt-9.1.0/include")
460+
add_compile_definitions(FMT_HEADER_ONLY)
461+
include_directories(SYSTEM ${FMT_INCLUDE_DIR})
462+
463+
459464
# see https://stackoverflow.com/questions/70898030/boost-link-error-using-conan-find-package
460465
if (MSVC)
461466
add_definitions(-DBOOST_ALL_NO_LIB)

src/util/log.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "util/isatty.hpp"
33
#include <boost/algorithm/string/predicate.hpp>
44
#include <cstdio>
5+
#include <fmt/chrono.h>
56
#include <iostream>
67
#include <mutex>
78
#include <string>
@@ -73,23 +74,33 @@ void Log::Init()
7374
if (!LogPolicy::GetInstance().IsMute() && level <= LogPolicy::GetInstance().GetLevel())
7475
{
7576
const bool is_terminal = IsStdoutATTY();
77+
78+
auto format = [is_terminal](const char *level, const char *color) {
79+
const auto timestamp = std::chrono::system_clock::now();
80+
return fmt::format("{}[{:%FT%H:%M:}{:%S}] [{}] ",
81+
is_terminal ? color : "",
82+
timestamp,
83+
timestamp.time_since_epoch(),
84+
level);
85+
};
86+
7687
switch (level)
7788
{
7889
case logNONE:
7990
break;
8091
case logWARNING:
81-
stream << (is_terminal ? YELLOW : "") << "[warn] ";
92+
stream << format("warn", YELLOW);
8293
break;
8394
case logERROR:
84-
stream << (is_terminal ? RED : "") << "[error] ";
95+
stream << format("error", RED);
8596
break;
8697
case logDEBUG:
8798
#ifdef ENABLE_DEBUG_LOGGING
88-
stream << (is_terminal ? MAGENTA : "") << "[debug] ";
99+
stream << format("debug", MAGENTA);
89100
#endif
90101
break;
91102
default: // logINFO:
92-
stream << "[info] ";
103+
stream << format("info", "");
93104
break;
94105
}
95106
}

third_party/fmt-9.1.0/.clang-format

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Run manually to reformat a file:
2+
# clang-format -i --style=file <file>
3+
Language: Cpp
4+
BasedOnStyle: Google
5+
IndentPPDirectives: AfterHash
6+
IndentCaseLabels: false
7+
AlwaysBreakTemplateDeclarations: false
8+
DerivePointerAlignment: false

0 commit comments

Comments
 (0)