diff options
Diffstat (limited to 'xbmc/utils/test/Testlog.cpp')
| -rw-r--r-- | xbmc/utils/test/Testlog.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/xbmc/utils/test/Testlog.cpp b/xbmc/utils/test/Testlog.cpp new file mode 100644 index 0000000..7405c02 --- /dev/null +++ b/xbmc/utils/test/Testlog.cpp | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "CompileInfo.h" | ||
| 10 | #include "ServiceBroker.h" | ||
| 11 | #include "filesystem/File.h" | ||
| 12 | #include "filesystem/SpecialProtocol.h" | ||
| 13 | #include "test/TestUtils.h" | ||
| 14 | #include "utils/RegExp.h" | ||
| 15 | #include "utils/StringUtils.h" | ||
| 16 | #include "utils/log.h" | ||
| 17 | |||
| 18 | #include <stdlib.h> | ||
| 19 | |||
| 20 | #include <gtest/gtest.h> | ||
| 21 | |||
| 22 | class Testlog : public testing::Test | ||
| 23 | { | ||
| 24 | protected: | ||
| 25 | Testlog() = default; | ||
| 26 | ~Testlog() override { CServiceBroker::GetLogging().Uninitialize(); } | ||
| 27 | }; | ||
| 28 | |||
| 29 | TEST_F(Testlog, Log) | ||
| 30 | { | ||
| 31 | std::string logfile, logstring; | ||
| 32 | char buf[100]; | ||
| 33 | ssize_t bytesread; | ||
| 34 | XFILE::CFile file; | ||
| 35 | CRegExp regex; | ||
| 36 | |||
| 37 | std::string appName = CCompileInfo::GetAppName(); | ||
| 38 | StringUtils::ToLower(appName); | ||
| 39 | logfile = CSpecialProtocol::TranslatePath("special://temp/") + appName + ".log"; | ||
| 40 | CServiceBroker::GetLogging().Initialize( | ||
| 41 | CSpecialProtocol::TranslatePath("special://temp/").c_str()); | ||
| 42 | EXPECT_TRUE(XFILE::CFile::Exists(logfile)); | ||
| 43 | |||
| 44 | CLog::Log(LOGDEBUG, "debug log message"); | ||
| 45 | CLog::Log(LOGINFO, "info log message"); | ||
| 46 | CLog::Log(LOGNOTICE, "notice log message"); | ||
| 47 | CLog::Log(LOGWARNING, "warning log message"); | ||
| 48 | CLog::Log(LOGERROR, "error log message"); | ||
| 49 | CLog::Log(LOGSEVERE, "severe log message"); | ||
| 50 | CLog::Log(LOGFATAL, "fatal log message"); | ||
| 51 | CLog::Log(LOGNONE, "none type log message"); | ||
| 52 | CServiceBroker::GetLogging().Uninitialize(); | ||
| 53 | |||
| 54 | EXPECT_TRUE(file.Open(logfile)); | ||
| 55 | while ((bytesread = file.Read(buf, sizeof(buf) - 1)) > 0) | ||
| 56 | { | ||
| 57 | buf[bytesread] = '\0'; | ||
| 58 | logstring.append(buf); | ||
| 59 | } | ||
| 60 | file.Close(); | ||
| 61 | EXPECT_FALSE(logstring.empty()); | ||
| 62 | |||
| 63 | EXPECT_STREQ("\xEF\xBB\xBF", logstring.substr(0, 3).c_str()); | ||
| 64 | |||
| 65 | EXPECT_TRUE(regex.RegComp(".*DEBUG <general>: debug log message.*")); | ||
| 66 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 67 | EXPECT_TRUE(regex.RegComp(".*INFO <general>: info log message.*")); | ||
| 68 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 69 | EXPECT_TRUE(regex.RegComp(".*INFO <general>: notice log message.*")); | ||
| 70 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 71 | EXPECT_TRUE(regex.RegComp(".*WARNING <general>: warning log message.*")); | ||
| 72 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 73 | EXPECT_TRUE(regex.RegComp(".*ERROR <general>: error log message.*")); | ||
| 74 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 75 | EXPECT_TRUE(regex.RegComp(".*FATAL <general>: severe log message.*")); | ||
| 76 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 77 | EXPECT_TRUE(regex.RegComp(".*FATAL <general>: fatal log message.*")); | ||
| 78 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 79 | EXPECT_TRUE(regex.RegComp(".*OFF <general>: none type log message.*")); | ||
| 80 | EXPECT_GE(regex.RegFind(logstring), 0); | ||
| 81 | |||
| 82 | EXPECT_TRUE(XFILE::CFile::Delete(logfile)); | ||
| 83 | } | ||
| 84 | |||
| 85 | TEST_F(Testlog, SetLogLevel) | ||
| 86 | { | ||
| 87 | std::string logfile; | ||
| 88 | |||
| 89 | std::string appName = CCompileInfo::GetAppName(); | ||
| 90 | StringUtils::ToLower(appName); | ||
| 91 | logfile = CSpecialProtocol::TranslatePath("special://temp/") + appName + ".log"; | ||
| 92 | CServiceBroker::GetLogging().Initialize( | ||
| 93 | CSpecialProtocol::TranslatePath("special://temp/").c_str()); | ||
| 94 | EXPECT_TRUE(XFILE::CFile::Exists(logfile)); | ||
| 95 | |||
| 96 | EXPECT_EQ(LOG_LEVEL_DEBUG, CServiceBroker::GetLogging().GetLogLevel()); | ||
| 97 | CServiceBroker::GetLogging().SetLogLevel(LOG_LEVEL_MAX); | ||
| 98 | EXPECT_EQ(LOG_LEVEL_MAX, CServiceBroker::GetLogging().GetLogLevel()); | ||
| 99 | |||
| 100 | CServiceBroker::GetLogging().Uninitialize(); | ||
| 101 | EXPECT_TRUE(XFILE::CFile::Delete(logfile)); | ||
| 102 | } | ||
