Symphony Of Empires
log.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // log.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include <cstdio>
26 #include <optional>
27 #include <memory>
28 #include "log.hpp"
29 
30 #ifdef E3D_LOG_TO_FILE
31 static std::optional<std::unique_ptr<FILE, int(*)(FILE*)>> log_fp;
32 #endif
33 #ifdef E3D_TARGET_SWITCH
34 struct PrintConsole;
35 extern "C" void consoleUpdate(PrintConsole* console);
36 #endif
37 
38 static bool debug_show = false;
39 
41 void Eng3D::Log::log(const std::string_view severity, const std::string_view category, const std::string_view msg) {
42  if(!debug_show && severity == "DEBUG") return;
43 #ifdef E3D_LOG_TO_FILE
44  if(!log_fp.has_value())
45  log_fp.emplace(std::unique_ptr<FILE, int (*)(FILE *)>(fopen("log.txt", "w+t"), fclose));
46  fprintf((*log_fp).get(), "<%s:%s> %s\n", severity.data(), category.data(), msg.data());
47 #else
48 # ifndef E3D_TARGET_SWITCH
49  printf("<%s:%s> %s\n", severity.data(), category.data(), msg.data());
50 # else
51  char tmpbuf[256];
52  snprintf(tmpbuf, sizeof(tmpbuf), "<%s:%s> %s", severity.c_str(), category.c_str(), msg.c_str());
53  fprintf(stderr, tmpbuf);
54 # endif
55 #endif
56 }
57 
58 void Eng3D::Log::debug(const std::string_view category, const std::string_view msg) {
59 #if defined E3D_DEBUG || 1
60  Eng3D::Log::log("DEBUG", category, msg);
61 #endif
62 }
63 
64 void Eng3D::Log::warning(const std::string_view category, const std::string_view msg) {
65  Eng3D::Log::log("WARN", category, msg);
66 }
67 
68 void Eng3D::Log::error(const std::string_view category, const std::string_view msg) {
69  Eng3D::Log::log("ERROR", category, msg);
70 }
void error(const std::string_view category, const std::string_view msg)
Definition: log.cpp:68
void debug(const std::string_view category, const std::string_view msg)
Definition: log.cpp:58
void log(const std::string_view severity, const std::string_view category, const std::string_view msg)
Logs data to a file or console.
Definition: log.cpp:41
void warning(const std::string_view category, const std::string_view msg)
Definition: log.cpp:64