Phoenix2D-Library  0.10
Logger.cpp
Go to the documentation of this file.
1 /*
2  * Phoenix2D (RoboCup Soccer Simulation 2D League)
3  * Copyright (c) 2013 Ivan Gonzalez
4  *
5  * This file is part of Phoenix2D.
6  *
7  * Phoenix2D is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Phoenix2D is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Phoenix2D. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <fstream>
22 #include <iostream>
23 #include <sstream>
24 #include <ctime>
25 #include "Logger.h"
26 #include "Self.h"
27 #include "Controller.h"
28 #include "Config.h"
29 namespace Phoenix
30 {
31 std::filebuf buf;
32 std::streambuf* oldbuf;
33 
35  logging = false;
36 }
37 
39  if (logging) {
40  std::clog.rdbuf(oldbuf);
41  buf.close();
42  }
43 }
44 
45 void Logger::log() {
46  logging = true;
47  std::stringstream ss;
48  time_t t = time(0); // get time now
49  struct tm* now = localtime(&t);
50  switch (Controller::AGENT_TYPE) {
51  case 'p':
52  case 'g':
53  ss << Self::TEAM_NAME << "_" << Self::UNIFORM_NUMBER << ".";
54  break;
55  case 't':
56  ss << "Trainer.";
57  break;
58  }
59  if (Config::LOG_NAME.length() > 0) {
60  ss << Config::LOG_NAME << ".";
61  }
62  if (now->tm_mday < 10) {
63  ss << "0" << now->tm_mday << ".";
64  } else {
65  ss << now->tm_mday << ".";
66  }
67  if (now->tm_mon < 10) {
68  ss << "0" << now->tm_mon << "-";
69  } else {
70  ss << now->tm_mon << "-";
71  }
72  if (now->tm_hour < 10) {
73  ss << "0" << now->tm_hour << ".";
74  } else {
75  ss << now->tm_hour << ".";
76  }
77  if (now->tm_min < 10) {
78  ss << "0" << now->tm_min << ".";
79  } else {
80  ss << now->tm_min << ".";
81  }
82  if (now->tm_sec < 10) {
83  ss << "0" << now->tm_sec;
84  } else {
85  ss << now->tm_sec;
86  }
87  ss << ".log" << std::endl;
88  std::string filename;
89  std::getline(ss, filename);
90  buf.open(filename.c_str(), std::ios::out);
91  oldbuf = std::clog.rdbuf(&buf);
92 }
93 }
static int UNIFORM_NUMBER
Definition: Self.h:49
void log()
Definition: Logger.cpp:45
bool logging
Definition: Logger.h:36
std::streambuf * oldbuf
Definition: Logger.cpp:32
static std::string LOG_NAME
Definition: Config.h:44
static std::string TEAM_NAME
Definition: Self.h:48
std::filebuf buf
Definition: Logger.cpp:31
static char AGENT_TYPE
p = Player, t = Trainer
Definition: Controller.h:78