libreoffice-online/test/testlog.hpp
Ashod Nakashian 1d37371944 wsd: minor improvements to logging and macros
This primarily combines the string-literals
that form the filename and line number to
reduce four calls to operator<< to only one.

Also fix LOK_ASSERT_STATE to use its argument.

Change-Id: I5e8eeb4f862fe8679b1e1f7a57412dc84dc30e92
Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2022-04-06 18:33:45 -04:00

102 lines
5.2 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <Log.hpp>
namespace helpers
{
inline std::chrono::steady_clock::time_point& getTestStartTime()
{
static auto TestStartTime = std::chrono::steady_clock::now();
return TestStartTime;
}
inline void resetTestStartTime() { getTestStartTime() = std::chrono::steady_clock::now(); }
inline std::chrono::milliseconds timeSinceTestStartMs()
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()
- getTestStartTime());
}
} // namespace helpers
//FIXME: use LOG_ macros and unify with the existing logging system.
// Oh dear std::cerr and/or its re-direction is not
// necessarily thread safe on Linux
// This is the canonical test log function.
inline void writeTestLog(const char* const p)
{
fputs(p, stderr);
fflush(stderr);
}
inline void writeTestLog(const std::string& s) { writeTestLog(s.c_str()); }
#ifdef TST_LOG_REDIRECT
void tstLog(const std::ostringstream& stream);
#else
inline void tstLog(const std::ostringstream& stream) { writeTestLog(stream.str()); }
#endif
#define TST_LOG_NAME_BEGIN(OSS, NAME, X, FLUSH) \
do \
{ \
char b_[1024]; \
OSS << Log::prefix<sizeof(b_) - 1>(b_, "TST") << NAME << " [" << __func__ << "] (+" \
<< helpers::timeSinceTestStartMs() << "): " << std::boolalpha << X; \
if (FLUSH) \
tstLog(OSS); \
} while (false)
#define TST_LOG_BEGIN(X) \
do \
{ \
std::ostringstream oss; \
TST_LOG_NAME_BEGIN(oss, testname, X, true); \
} while (false)
#define TST_LOG_APPEND(X) \
do \
{ \
std::ostringstream str; \
str << X; \
tstLog(str); \
} while (false)
#define TST_LOG_END_X(OSS) \
do \
{ \
OSS << "| " __FILE__ ":" STRING(__LINE__) "\n"; \
tstLog(OSS); \
} while (false)
#define TST_LOG_END \
do \
{ \
std::ostringstream oss_log_end; \
TST_LOG_END_X(oss_log_end); \
} while (false)
#define TST_LOG_NAME(NAME, X) \
do \
{ \
std::ostringstream oss_log_name; \
TST_LOG_NAME_BEGIN(oss_log_name, NAME, X, false); \
TST_LOG_END_X(oss_log_name); \
} while (false)
/// Used by the "old-style" tests. FIXME: Unify.
#define TST_LOG(X) TST_LOG_NAME(testname, X)
/// Used by the "new-style" tests. FIXME: Unify.
#define LOG_TST(X) TST_LOG(X)
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */