[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / utils / test / Testlog.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "utils/log.h"
22 #include "utils/RegExp.h"
23 #include "filesystem/File.h"
24 #include "filesystem/SpecialProtocol.h"
25
26 #include "test/TestUtils.h"
27
28 #include "gtest/gtest.h"
29
30 class Testlog : public testing::Test
31 {
32 protected:
33   Testlog(){}
34   ~Testlog()
35   {
36     /* Reset globals used by CLog after each test. */
37     g_log_globalsRef->m_file = NULL;
38     g_log_globalsRef->m_repeatCount = 0;
39     g_log_globalsRef->m_repeatLogLevel = -1;
40     g_log_globalsRef->m_logLevel = LOG_LEVEL_DEBUG;
41   }
42 };
43
44 TEST_F(Testlog, Log)
45 {
46   CStdString logfile, logstring;
47   char buf[100];
48   unsigned int bytesread;
49   XFILE::CFile file;
50   CRegExp regex;
51
52   logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log";
53   EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/")));
54   EXPECT_TRUE(XFILE::CFile::Exists(logfile));
55
56   CLog::Log(LOGDEBUG, "debug log message");
57   CLog::Log(LOGINFO, "info log message");
58   CLog::Log(LOGNOTICE, "notice log message");
59   CLog::Log(LOGWARNING, "warning log message");
60   CLog::Log(LOGERROR, "error log message");
61   CLog::Log(LOGSEVERE, "severe log message");
62   CLog::Log(LOGFATAL, "fatal log message");
63   CLog::Log(LOGNONE, "none type log message");
64   CLog::Close();
65
66   EXPECT_TRUE(file.Open(logfile));
67   while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0)
68   {
69     buf[bytesread] = '\0';
70     logstring.append(buf);
71   }
72   file.Close();
73   EXPECT_FALSE(logstring.empty());
74
75   EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str());
76
77   EXPECT_TRUE(regex.RegComp(".*DEBUG: debug log message.*"));
78   EXPECT_GE(regex.RegFind(logstring), 0);
79   EXPECT_TRUE(regex.RegComp(".*INFO: info log message.*"));
80   EXPECT_GE(regex.RegFind(logstring), 0);
81   EXPECT_TRUE(regex.RegComp(".*NOTICE: notice log message.*"));
82   EXPECT_GE(regex.RegFind(logstring), 0);
83   EXPECT_TRUE(regex.RegComp(".*WARNING: warning log message.*"));
84   EXPECT_GE(regex.RegFind(logstring), 0);
85   EXPECT_TRUE(regex.RegComp(".*ERROR: error log message.*"));
86   EXPECT_GE(regex.RegFind(logstring), 0);
87   EXPECT_TRUE(regex.RegComp(".*SEVERE: severe log message.*"));
88   EXPECT_GE(regex.RegFind(logstring), 0);
89   EXPECT_TRUE(regex.RegComp(".*FATAL: fatal log message.*"));
90   EXPECT_GE(regex.RegFind(logstring), 0);
91   EXPECT_TRUE(regex.RegComp(".*NONE: none type log message.*"));
92   EXPECT_GE(regex.RegFind(logstring), 0);
93
94   EXPECT_TRUE(XFILE::CFile::Delete(logfile));
95 }
96
97 TEST_F(Testlog, MemDump)
98 {
99   CStdString logfile, logstring;
100   char buf[100];
101   unsigned int bytesread;
102   XFILE::CFile file;
103   CRegExp regex;
104   char refdata[] = "0123456789abcdefghijklmnopqrstuvwxyz";
105
106   logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log";
107   EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/")));
108   EXPECT_TRUE(XFILE::CFile::Exists(logfile));
109
110   CLog::MemDump(refdata, sizeof(refdata));
111   CLog::Close();
112
113   EXPECT_TRUE(file.Open(logfile));
114   while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0)
115   {
116     buf[bytesread] = '\0';
117     logstring.append(buf);
118   }
119   file.Close();
120   EXPECT_FALSE(logstring.empty());
121
122   EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str());
123
124   EXPECT_TRUE(regex.RegComp(".*DEBUG: MEM_DUMP: Dumping from.*"));
125   EXPECT_GE(regex.RegFind(logstring), 0);
126   EXPECT_TRUE(regex.RegComp(".*DEBUG: MEM_DUMP: 0000  30 31 32 33.*"));
127   EXPECT_GE(regex.RegFind(logstring), 0);
128   EXPECT_TRUE(regex.RegComp(".*73 74 75 76  ghijklmnopqrstuv.*"));
129   EXPECT_GE(regex.RegFind(logstring), 0);
130
131   EXPECT_TRUE(XFILE::CFile::Delete(logfile));
132 }
133
134 TEST_F(Testlog, SetLogLevel)
135 {
136   CStdString logfile;
137
138   logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log";
139   EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/")));
140   EXPECT_TRUE(XFILE::CFile::Exists(logfile));
141
142   EXPECT_EQ(LOG_LEVEL_DEBUG, CLog::GetLogLevel());
143   CLog::SetLogLevel(LOG_LEVEL_MAX);
144   EXPECT_EQ(LOG_LEVEL_MAX, CLog::GetLogLevel());
145
146   CLog::Close();
147   EXPECT_TRUE(XFILE::CFile::Delete(logfile));
148 }