finish move to Python-handled imports
[Tsunagari.git] / src / log.cpp
blob11481e0eb84ecd9a41e149a0edb503e0633cd52e
1 /***************************************
2 ** Tsunagari Tile Engine **
3 ** log.cpp **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
7 // **********
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // **********
27 #include <iostream>
29 #include <Gosu/Timing.hpp>
31 #include "client-conf.h"
32 #include "log.h"
33 #include "python.h"
34 #include "python-bindings-template.cpp"
35 #include "world.h"
37 #ifdef _WIN32
38 #include "os-windows.h"
39 #endif
41 #ifdef __APPLE__
42 #include "os-mac.h"
43 #endif
45 static verbosity_t verb = V_NORMAL;
47 static unsigned long startTime;
49 static std::string& chomp(std::string& str)
51 std::string::size_type notwhite = str.find_last_not_of(" \t\n\r");
52 str.erase(notwhite + 1);
53 return str;
56 static std::string ts()
58 unsigned long now = Gosu::milliseconds();
60 std::ostringstream ts;
61 ts.precision(3);
62 ts << std::fixed;
63 ts << (now - startTime) / (long double)1000.0;
64 return "[" + ts.str() + "] ";
67 bool Log::init()
69 startTime = Gosu::milliseconds();
70 return true;
73 void Log::setVerbosity(verbosity_t v)
75 verb = v;
78 void Log::info(std::string domain, std::string msg)
80 std::string str = ts() + "Info [" + domain + "] - " + chomp(msg);
81 if (verb > V_NORMAL)
82 std::cout << str << std::endl;
85 void Log::err(std::string domain, std::string msg)
87 if (conf.halting == HALT_ERROR) {
88 Log::fatal(domain, msg);
89 exit(1);
91 std::string str = ts() + "Error [" + domain + "] - " + chomp(msg);
92 if (inPythonScript) {
93 PyErr_SetString(PyExc_RuntimeError, str.c_str());
95 else {
96 if (verb > V_QUIET) {
97 std::cerr << str << std::endl;
98 #ifdef _WIN32
99 wMessageBox("Tsunagari - Error", str);
100 #endif
101 #ifdef __APPLE__
102 macMessageBox("Tsunagari - Error", str.c_str());
103 #endif
108 void Log::fatal(std::string domain, std::string msg)
110 std::string str = ts() + "Fatal [" + domain + "] - " + chomp(msg);
111 if (inPythonScript) {
112 PyErr_SetString(PyExc_RuntimeError, str.c_str());
114 else {
115 std::cerr << str << std::endl;
116 #ifdef _WIN32
117 wMessageBox("Tsunagari - Fatal", str);
118 #endif
119 #ifdef __APPLE__
120 macMessageBox("Tsunagari - Fatal", str.c_str());
121 #endif
125 void Log::reportVerbosityOnStartup()
127 std::string verbString;
128 switch (conf.verbosity)
130 case V_QUIET:
131 verbString = "QUIET";
132 break;
133 case V_NORMAL:
134 verbString = "NORMAL";
135 break;
136 case V_VERBOSE:
137 verbString = "VERBOSE";
138 break;
140 std::cout << ts() << "Reporting engine messages in " << verbString
141 << " mode." << std::endl;
144 static void pythonLogInfo(std::string msg)
146 Log::info("Script", msg);
149 void exportLog()
151 pythonAddFunction("log", pythonLogInfo);