direcs  2012-09-30
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
logfile.cpp
Go to the documentation of this file.
1 /*************************************************************************
2  * Copyright (C) Markus Knapp *
3  * www.direcs.de *
4  * *
5  * This file is part of direcs. *
6  * *
7  * direcs is free software: you can redistribute it and/or modify it *
8  * under the terms of the GNU General Public License as published *
9  * by the Free Software Foundation, version 3 of the License. *
10  * *
11  * direcs is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with direcs. If not, see <http://www.gnu.org/licenses/>. *
18  * *
19  *************************************************************************/
20 
21 #include "logfile.h"
22 
24 {
25  logFilename = "not_set";
26 }
27 
28 
30 {
31 }
32 
33 /*
34 QString Logfile::checkPath()
35 {
36  //
37  // check the current path from which the program started
38  //
39 
40  // get the current path and store it
41  programPath = QDir::currentPath();
42 
43  return programPath;
44 }
45 
46 
47 bool Logfile::checkFiles()
48 {
49  QString filename;
50 
51 
52  if (filename == "not_set")
53  {
54  return false;
55  }
56 
57 
58  // path + filename for ini-file
59  filename = programPath;
60  filename.append("/");
61  filename.append(iniFilename);
62 
63  // check if ini-file exists
64  if (QFile::exists(filename) == false)
65  {
66  return false;
67  }
68 
69 
70  return true;
71 }
72 */
73 
74 void Logfile::appendLog(QString text, bool CR)
75 {
76  Q_UNUSED(CR); // temporarily not in use
77 
78 
79  if (logFilename == "not_set")
80  {
81  qDebug("logFilename not set!");
82  return;
83  }
84 
85 
86  // remove HTML
87  if (text.contains("<") && text.contains(">"))
88  {
89  int start= -1;
90 
91  while (text.contains(">"))
92  {
93  // search for the first HTML "<"
94  start = text.indexOf("<");
95 
96  // when found
97  if (start != 1)
98  {
99  text.remove(start, (text.indexOf(">") - start + 1) );
100  }
101  else
102  break; // leave while loop, when we only found an '>' without leading '<'
103  };
104  }
105 
106 
107  now = QDateTime::currentDateTime(); // get the current date and time
108  QTextStream out(&file);
109  out << now.toString("yyyy") << "-" << now.toString("MM") << "-" << now.toString("dd") << " " << now.toString("hh") << ":" << now.toString("mm") << ":" << now.toString("ss") << "." << now.toString("zzz") << " " << text << "\n";
110 }
111 
112 
113 void Logfile::setFilename(QString filename)
114 {
115  // set the filename
116  logFilename = filename;
117  file.setFileName(filename);
118 
119  if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
120  {
121  qDebug("Error opening logfile!");
122  return;
123  }
124 }
125 
126 
127 void Logfile::writeHeartbeat(unsigned char state)
128 {
129  Q_UNUSED(state);
130 
131  appendLog("-heartbeat-");
132 }