pd_scopelogger.cpp
Go to the documentation of this file.
1 /** @file pd_scopelogger.cpp Create Logfile */
2 
3 /* Pushdown plugin for FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2014 Ramon Barakat, Stefan Jacobi, Sven Schneider, Anne-Kathrin Hess
6 
7 */
8 
9 #include "pd_scopelogger.h"
10 
11 namespace faudes {
12 
13 /********************************************************************
14 
15  Implementation of ScopeLogger
16 
17  ********************************************************************/
18 
19 //Get current date/time, format is YYYY-MM-DD.HH:mm:ss
20 const std::string currentDateTime() {
21  time_t now = time(0);
22  struct tm tstruct;
23  char buf[80];
24  tstruct = *localtime(&now);
25  strftime(buf, sizeof(buf), "%Y-%m-%d: %X", &tstruct);
26 
27  return buf;
28 }
29 
30 // ****************************
31 // class ScopeLogger
32 // ***************************
33 
34 int ScopeLogger::indent = 0;
36 std::vector<ScopeLogger*> ScopeLogger::vFunTrace;
37 std::stringstream ScopeLogger::stFunDone;
38 time_t ScopeLogger::starttime = 0;
39 
40 //constructor
41 ScopeLogger::ScopeLogger(std::string const & msg) :
42  msg(msg), startFuntime(time(0)) {
43 
44  if (starttime == 0)
45  starttime = time(0);
46 
47  outputFile << std::string(indent++, ' ') << "> " << msg << " ("
48  << currentDateTime() << ") " << std::endl;
49  vFunTrace.push_back(this);
50 }
51 
52 //deconstructor
54  int totalTime = static_cast<int>(difftime(time(0), starttime));
55  std::stringstream stotalTime;
56  stotalTime << totalTime / 60 << "min " << totalTime % 60 << "s";
57 
58  int funTime = static_cast<int>(difftime(time(0), startFuntime));
59  std::stringstream sfunTime;
60  sfunTime << funTime / 60 << "min " << funTime % 60 << "s";
61 
62  if (!vFunTrace.empty())
63  vFunTrace.pop_back();
64 
65  if (stFunDone.str().empty())
66  stFunDone << "Done: ";
67  stFunDone << msg << "(" << totalTime << "s)";
68 
69  outputFile << std::string(--indent, ' ') << "< " << msg << " ("
70  << currentDateTime() << ") function " << sfunTime.str() << ", total: "
71  << stotalTime.str() << std::endl;
72 }
73 
74 //print stack trace
76 
77  if (!ScopeLogger::stFunDone.str().empty())
78  std::cout << ScopeLogger::stFunDone.str() << std::endl;
79 
80  if (ScopeLogger::vFunTrace.empty())
81  return;
82 
83  std::stringstream trace;
84  trace << "Trace: ";
85 
86  for (std::vector<ScopeLogger*>::iterator ilog =
87  ScopeLogger::vFunTrace.begin();
88  ilog != ScopeLogger::vFunTrace.end(); ++ilog) {
89  trace << (*ilog)->msg << "(" << difftime(time(0), (*ilog)->startFuntime)
90  << "s) ";
91  }
92 
93  std::cout << trace.str() << std::endl;
94 }
95 
96 } // namespace faudes

libFAUDES 2.28c --- 2016.09.30 --- c++ api documentaion by doxygen