2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
21 #include "utils/log.h"
22 #include "utils/RegExp.h"
23 #include "filesystem/File.h"
24 #include "filesystem/SpecialProtocol.h"
26 #include "test/TestUtils.h"
28 #include "gtest/gtest.h"
30 class Testlog : public testing::Test
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;
46 CStdString logfile, logstring;
48 unsigned int bytesread;
52 logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log";
53 EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/")));
54 EXPECT_TRUE(XFILE::CFile::Exists(logfile));
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");
66 EXPECT_TRUE(file.Open(logfile));
67 while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0)
69 buf[bytesread] = '\0';
70 logstring.append(buf);
73 EXPECT_FALSE(logstring.empty());
75 EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str());
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);
94 EXPECT_TRUE(XFILE::CFile::Delete(logfile));
97 TEST_F(Testlog, MemDump)
99 CStdString logfile, logstring;
101 unsigned int bytesread;
104 char refdata[] = "0123456789abcdefghijklmnopqrstuvwxyz";
106 logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log";
107 EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/")));
108 EXPECT_TRUE(XFILE::CFile::Exists(logfile));
110 CLog::MemDump(refdata, sizeof(refdata));
113 EXPECT_TRUE(file.Open(logfile));
114 while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0)
116 buf[bytesread] = '\0';
117 logstring.append(buf);
120 EXPECT_FALSE(logstring.empty());
122 EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str());
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);
131 EXPECT_TRUE(XFILE::CFile::Delete(logfile));
134 TEST_F(Testlog, SetLogLevel)
138 logfile = CSpecialProtocol::TranslatePath("special://temp/") + "xbmc.log";
139 EXPECT_TRUE(CLog::Init(CSpecialProtocol::TranslatePath("special://temp/")));
140 EXPECT_TRUE(XFILE::CFile::Exists(logfile));
142 EXPECT_EQ(LOG_LEVEL_DEBUG, CLog::GetLogLevel());
143 CLog::SetLogLevel(LOG_LEVEL_MAX);
144 EXPECT_EQ(LOG_LEVEL_MAX, CLog::GetLogLevel());
147 EXPECT_TRUE(XFILE::CFile::Delete(logfile));