os.h
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 #pragma once
22 
23 #include <momemta/impl/logger/common.h>
24 
25 #include <chrono>
26 #include <ctime>
27 #include <sys/syscall.h>
28 #include <thread>
29 #include <unistd.h>
30 
31 namespace logger {
32 
33 namespace details {
34 
35 namespace os {
36 
37 inline log_clock::time_point now() {
38  return log_clock::now();
39 }
40 
41 inline std::tm localtime(const std::time_t& time_tt) {
42  std::tm tm;
43  localtime_r(&time_tt, &tm);
44 
45  return tm;
46 }
47 
48 inline std::tm localtime() {
49  std::time_t now_t = time(nullptr);
50  return localtime(now_t);
51 }
52 
53 inline size_t thread_id() {
54  return static_cast<size_t>(syscall(SYS_gettid));
55 }
56 
57 #if !defined (LOGGER_EOL)
58 #define LOGGER_EOL "\n"
59 #endif
60 
61 constexpr static const char* eol = LOGGER_EOL;
62 constexpr static int eol_size = sizeof(LOGGER_EOL) - 1;
63 
64 }
65 
66 }
67 
68 }