LIBEUNOMIA
読み取り中…
検索中…
一致する文字列を見つけられません
debuglogger.h
[詳解]
1/*
2 * Copyright 2021 oZ/acy (名賀月晃嗣)
3 *
4 * Redistribution and use in source and binary forms,
5 * with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
37#ifndef INCLUDE_GUARD_EUNOMIA_DEBUG_LOGGER_H
38#define INCLUDE_GUARD_EUNOMIA_DEBUG_LOGGER_H
39
40#include <filesystem>
41#include <iostream>
42#include <fstream>
43#include <ctime>
44
45
47{
48
49namespace implement_
50{
58{
59public:
60 class Timestamp_{};
61
62private:
63 DebugLogger_() = default;
64
65#ifdef NDEBUG
66
67public:
68 template<typename T>
69 DebugLogger_& operator<<(const T& x) { return *this; }
70
71 template<typename T>
72 DebugLogger_& operator<<(const T* x) { return *this; }
73
74 DebugLogger_& operator<<(std::ostream& (*mnp)(std::ostream&))
75 { return *this; }
76
77 DebugLogger_& operator<<(const Timestamp_& ts) { return *this; }
78
79 static DebugLogger_& out()
80 {
81 static DebugLogger_ d;
82 return d;
83 }
84
85 static void set(const std::filesystem::path& path) {}
86
87
88#else // ifndef NDEBUG
89
90private:
91 static inline std::filesystem::path path_ = "eunomiaDebugLog.txt";
92 static inline std::ofstream ofs_;
93
94public:
95 template<typename T>
97 {
98 ofs_ << x;
99 return *this;
100 }
101
102 template<typename T>
104 {
105 ofs_ << x;
106 return *this;
107 }
108
109 DebugLogger_& operator<<(std::ostream& (*mnp)(std::ostream&))
110 {
111 ofs_ << mnp;
112 return *this;
113 }
114
116 {
117 auto now = std::time(nullptr);
118 ofs_ << std::put_time(std::localtime(&now), "[%Y/%m/%d %H:%M:%S] ");
119 return *this;
120 }
121
123 {
124 static DebugLogger_ d;
125 if (!ofs_.is_open())
126 ofs_.open(path_, std::ios::app);
127 return d;
128 }
129
130 static void set(const std::filesystem::path& path) { path_ = path; }
131#endif
132};
133
134
135}//end of namespace implement_
136
137
142
143
148
149
150inline void setLoggingFile(const std::filesystem::path& path)
151{
153}
154
155
156}//end of namespace eunomia::debug
157
158
159
160
161#endif // INCLUDE_GUARD_EUNOMIA_DEBUG_LOGGER_H
デバッグログ出力ストリーム
Definition debuglogger.h:58
static void set(const std::filesystem::path &path)
Definition debuglogger.h:130
static DebugLogger_ & out()
Definition debuglogger.h:122
DebugLogger_ & operator<<(const Timestamp_ &ts)
Definition debuglogger.h:115
DebugLogger_ & operator<<(const T &x)
Definition debuglogger.h:96
DebugLogger_ & operator<<(const T *x)
Definition debuglogger.h:103
DebugLogger_ & operator<<(std::ostream &(*mnp)(std::ostream &))
Definition debuglogger.h:109
Definition debuglogger.h:47
implement_::DebugLogger_::Timestamp_ timestamp()
Definition debuglogger.h:144
void setLoggingFile(const std::filesystem::path &path)
Definition debuglogger.h:150
implement_::DebugLogger_ & out()
Definition debuglogger.h:138