OSDN Git Service

Happy new year 2017!
[mutilities/MUtilities.git] / test / src / Main.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 //Google Test
23 #include <gtest/gtest.h>
24
25 //MUtils
26 #include <MUtils/Global.h>
27 #include <MUtils/Version.h>
28
29 //CRT
30 #include <cstdio>
31 #include <cstdlib>
32
33 //===========================================================================
34 // Message Handler
35 //===========================================================================
36
37 static FILE* g_logFile = NULL;
38
39 static void initialize_mutils_log_file(const int argc, const wchar_t *const *const argv)
40 {
41         wchar_t basePath[_MAX_PATH], logFilePath[_MAX_PATH];
42         char gtestOutputPath[_MAX_PATH + 16];
43
44         wcscpy_s(basePath, _MAX_PATH, L"MUtilsTest");
45         if ((argc > 0) && argv[0] && argv[0][0])
46         {
47                 wcsncpy_s(basePath, _MAX_PATH, argv[0], _TRUNCATE);
48         }
49
50         _snwprintf_s(logFilePath, _MAX_PATH, _TRUNCATE, L"%s.log", basePath);
51         if (_wfopen_s(&g_logFile, logFilePath, L"w"))
52         {
53                 fprintf(stderr, "Failed to open log file for writing!\n");
54                 g_logFile = NULL;
55         }
56
57         _snprintf_s(gtestOutputPath, _MAX_PATH + 16, _TRUNCATE, "xml:%S.xml", basePath);
58         if (gtestOutputPath[0] && (!strchr(gtestOutputPath, '?')))
59         {
60                 ::testing::GTEST_FLAG(output) = std::string(gtestOutputPath);
61         }
62 }
63
64 static bool get_time_stamp(char *const buffer, const size_t buff_size)
65 {
66         const time_t time_stamp = time(NULL);
67         struct tm tm_info;
68         if(localtime_s(&tm_info, &time_stamp))
69         {
70                 buffer[0] = L'\0';
71                 return false;
72         }
73         const size_t ret = strftime(buffer, buff_size, "%Y-%m-%d %H:%M:%S", &tm_info);
74         return (ret > 0) && (ret < buff_size);
75 }
76
77 static void qt_message_handler(QtMsgType type, const char *const msg)
78 {
79 #if defined(MUTILS_DEBUG) && MUTILS_DEBUG
80         if (msg && msg[0])
81         {
82                 fprintf(stderr, "%s\n", msg);
83         }
84 #endif //MUTILS_DEBUG
85         if (g_logFile && (!ferror(g_logFile)))
86         {
87                 char time_buffer[32];
88                 if (get_time_stamp(time_buffer, 32))
89                 {
90                         fprintf(g_logFile, "[%s] %s\n", time_buffer, msg);
91                 }
92                 else
93                 {
94                         fprintf(g_logFile, "%s\n", msg);
95                 }
96         }
97 }
98
99 //===========================================================================
100 // Main function
101 //===========================================================================
102
103 int wmain(int argc, wchar_t **argv)
104 {
105         printf("MuldeR's Utilities for Qt v%u.%02u - Regression Test Suite [%s]\n", MUtils::Version::lib_version_major(), MUtils::Version::lib_version_minor(), MUTILS_DEBUG ? "DEBUG" : "RELEASE");
106         printf("Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>. Some rights reserved.\n");
107         printf("Built on %s at %s with %s for Win-%s.\n\n", MUTILS_UTF8(MUtils::Version::lib_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::lib_build_time().toString(Qt::ISODate)), MUtils::Version::compiler_version(), MUtils::Version::compiler_arch());
108
109         printf("This library is free software; you can redistribute it and/or\n");
110         printf("modify it under the terms of the GNU Lesser General Public\n");
111         printf("License as published by the Free Software Foundation; either\n");
112         printf("version 2.1 of the License, or (at your option) any later version.\n\n");
113
114         initialize_mutils_log_file(argc, argv);
115         qInstallMsgHandler(qt_message_handler);
116         ::testing::InitGoogleTest(&argc, argv);
117
118         return RUN_ALL_TESTS();
119 }