Commit 0d9d7356 authored by captainwong's avatar captainwong

test logstream

parent 969f35f0
......@@ -99,7 +99,7 @@ private:
static OutputFunc outputFunc_;
static FlushFunc flushFunc_;
static LogLevel logLevel_;
static TimeZone timeZone_;
static const TimeZone* timeZone_;
Impl impl_;
};
......@@ -175,7 +175,7 @@ static constexpr unsigned int T_TIME_STR_LEN = 17;
Logger::OutputFunc Logger::outputFunc_ = detail::defaultOutput;
Logger::FlushFunc Logger::flushFunc_ = detail::defaultFlush;
Logger::LogLevel Logger::logLevel_ = detail::initLogLevel();
TimeZone Logger::timeZone_ = {};
const jlib::TimeZone* Logger::timeZone_ = date::locate_zone("Etc/UTC");
/******** LogStream operators *********/
......@@ -214,30 +214,32 @@ Logger::Impl::Impl(LogLevel level, int old_errno, const SourceFile& file, int li
void Logger::Impl::formatTime()
{
int64_t microSecsSinceEpoch = time_.microSecondsSinceEpoch();
time_t seconds = static_cast<time_t>(microSecsSinceEpoch / MICRO_SECONDS_PER_SECOND);
int microsecs = static_cast<int>(microSecsSinceEpoch % MICRO_SECONDS_PER_SECOND);
if (seconds != detail::t_lastSecond) {
detail::t_lastSecond = seconds;
struct tm tm_time;
if (timeZone_.valid()) {
// TODO
} else {
gmtime_r(&seconds, &tm_time);
}
int len = snprintf(detail::t_time, sizeof(detail::t_time), "%4d%02d%02d %02d:%02d:%02d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
assert(len == detail::T_TIME_STR_LEN); (void)len;
}
if (timeZone_.valid()) {
// TODO
} else {
Format us(".%06dZ ", microsecs); assert(us.length() == 9);
stream_ << detail::T(detail::t_time, detail::T_TIME_STR_LEN) << detail::T(us.data(), 9);
}
//int64_t microSecsSinceEpoch = time_.microSecondsSinceEpoch();
//time_t seconds = static_cast<time_t>(microSecsSinceEpoch / MICRO_SECONDS_PER_SECOND);
//int microsecs = static_cast<int>(microSecsSinceEpoch % MICRO_SECONDS_PER_SECOND);
//if (seconds != detail::t_lastSecond) {
// detail::t_lastSecond = seconds;
// struct tm tm_time;
// if (timeZone_.valid()) {
// // TODO
// } else {
// gmtime_r(&seconds, &tm_time);
// }
// int len = snprintf(detail::t_time, sizeof(detail::t_time), "%4d%02d%02d %02d:%02d:%02d",
// tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
// tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
// assert(len == detail::T_TIME_STR_LEN); (void)len;
//}
//if (timeZone_.valid()) {
// // TODO
//} else {
// Format us(".%06dZ ", microsecs); assert(us.length() == 9);
// stream_ << detail::T(detail::t_time, detail::T_TIME_STR_LEN) << detail::T(us.data(), 9);
//}
stream_ << format("%F %T(%Z) ", time_);
}
void Logger::Impl::finish()
......
#pragma once
#include "config.h"
#include "noncopyable.h"
#include "stringpiece.h"
#include "cast.h"
......@@ -36,21 +37,12 @@ private:
char *cur_;
public:
FixedBuffer()
: cur_(data_)
{
setCookie(cookieStart);
}
~FixedBuffer()
{
setCookie(cookieEnd);
}
FixedBuffer() : cur_(data_) { setCookie(cookieStart); }
~FixedBuffer() { setCookie(cookieEnd); }
void append(const char* buf, size_t len) {
if (implicit_cast<size_t>(avail()) > len) {
memcpy(cur_, buf, len);
cur_ += len;
memcpy(cur_, buf, len); cur_ += len;
}
}
......@@ -90,16 +82,12 @@ inline size_t convert(char buf[], T value)
T i = value;
char *p = buf;
do
{
do {
int lsd = static_cast<int>(i % 10);
i /= 10;
*p++ = zero[lsd];
i /= 10; *p++ = zero[lsd];
} while (i != 0);
if (value < 0) {
*p++ = '-';
}
if (value < 0) { *p++ = '-'; }
*p = '\0';
std::reverse(buf, p);
......@@ -114,11 +102,9 @@ static size_t convertHex(char buf[], uintptr_t value)
uintptr_t i = value;
char *p = buf;
do
{
do {
int lsd = static_cast<int>(i % 16);
i /= 16;
*p++ = digitsHex[lsd];
i /= 16; *p++ = digitsHex[lsd];
} while (i != 0);
*p = '\0';
......@@ -156,18 +142,14 @@ public:
uintptr_t v = reinterpret_cast<uintptr_t>(p);
if (buffer_.avail() >= MAX_NUMERIC_SIZE) {
char* buf = buffer_.current();
buf[0] = '0';
buf[1] = 'x';
buf[0] = '0'; buf[1] = 'x';
size_t len = detail::convertHex(buf + 2, v);
buffer_.add(len + 2);
}
return *this;
}
self& operator<<(float v) {
*this << static_cast<double>(v);
return *this;
}
self& operator<<(float v) { *this << static_cast<double>(v); return *this; }
self& operator<<(double v) {
if (buffer_.avail() >= MAX_NUMERIC_SIZE) {
......@@ -179,41 +161,21 @@ public:
// self& operator<<(long double);
self& operator<<(char v) {
buffer_.append(&v, 1);
return *this;
}
self& operator<<(char v) { buffer_.append(&v, 1); return *this; }
// self& operator<<(signed char);
// self& operator<<(unsigned char);
self& operator<<(const char *str) {
if (str) {
buffer_.append(str, strlen(str));
} else {
buffer_.append("(null)", 6);
}
return *this;
}
self& operator<<(const unsigned char *str) {
return operator<<(reinterpret_cast<const char *>(str));
}
self& operator<<(const std::string& v) {
buffer_.append(v.c_str(), v.size());
return *this;
}
self& operator<<(const StringPiece& v) {
buffer_.append(v.data(), v.size());
if (str) { buffer_.append(str, strlen(str));
} else { buffer_.append("(null)", 6); }
return *this;
}
self& operator<<(const Buffer& v) {
*this << v.toStringPiece();
return *this;
}
self& operator<<(const unsigned char *str) { return operator<<(reinterpret_cast<const char *>(str)); }
self& operator<<(const std::string& v) { buffer_.append(v.c_str(), v.size()); return *this; }
self& operator<<(const StringPiece& v) { buffer_.append(v.data(), v.size()); return *this; }
self& operator<<(const Buffer& v) { *this << v.toStringPiece(); return *this; }
void append(const char *data, int len) { buffer_.append(data, len); }
const Buffer& buffer() const { return buffer_; }
......@@ -266,7 +228,6 @@ private:
// Explicit instantiations
template Format::Format(const char* fmt, char);
template Format::Format(const char* fmt, short);
template Format::Format(const char* fmt, unsigned short);
template Format::Format(const char* fmt, int);
......@@ -275,7 +236,6 @@ template Format::Format(const char* fmt, long);
template Format::Format(const char* fmt, unsigned long);
template Format::Format(const char* fmt, long long);
template Format::Format(const char* fmt, unsigned long long);
template Format::Format(const char* fmt, float);
template Format::Format(const char* fmt, double);
......
......@@ -43,7 +43,6 @@ public:
#ifdef JLIB_WINDOWS
#include "3rdparty/spdlog/sinks/msvc_sink.h"
#include "utf8.h"
#endif // JLIB_WINDOWS
#include "utf8.h"
......
......@@ -238,6 +238,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_ping", "test_ping\test
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_date", "test_date\test_date.vcxproj", "{2252B5D4-2D13-4F02-8008-AB2E0D38A154}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_logstream", "test_logstream\test_logstream.vcxproj", "{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
......@@ -396,6 +398,14 @@ Global
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x64.Build.0 = Release|x64
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x86.ActiveCfg = Release|Win32
{2252B5D4-2D13-4F02-8008-AB2E0D38A154}.Release|x86.Build.0 = Release|Win32
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Debug|x64.ActiveCfg = Debug|x64
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Debug|x64.Build.0 = Debug|x64
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Debug|x86.ActiveCfg = Debug|Win32
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Debug|x86.Build.0 = Debug|Win32
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Release|x64.ActiveCfg = Release|x64
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Release|x64.Build.0 = Release|x64
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Release|x86.ActiveCfg = Release|Win32
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -441,6 +451,7 @@ Global
{EAAE08F7-6CD8-4708-9FD4-ADD856CC6658} = {21DC893D-AB0B-48E1-9E23-069A025218D9}
{26D2B628-713F-419A-B2E2-32BDFA803E3C} = {21DC893D-AB0B-48E1-9E23-069A025218D9}
{2252B5D4-2D13-4F02-8008-AB2E0D38A154} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61} = {D9BC4E5B-7E8F-4C86-BF15-CCB75CBC256F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A8EBEA58-739C-4DED-99C0-239779F57D5D}
......
......@@ -89,9 +89,12 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(BOOST);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
<PreprocessorDefinitions>HAS_UNCAUGHT_EXCEPTIONS=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>G:\dev_libs\date\build\Debug\tz.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
......
#include "../../jlib/base/logstream.h"
#include "../../jlib/base/timestamp.h"
#include <stdio.h>
using namespace jlib;
const size_t N = 1000000;
template<typename T>
void benchPrintf(const char* fmt)
{
char buf[32];
Timestamp start(nowTimestamp());
for (size_t i = 0; i < N; ++i)
snprintf(buf, sizeof buf, fmt, (T)(i));
Timestamp end(nowTimestamp());
printf("benchPrintf %s\n", format("%S", (end - start)).c_str());
}
template<typename T>
void benchStringStream()
{
Timestamp start(nowTimestamp());
std::ostringstream os;
for (size_t i = 0; i < N; ++i) {
os << (T)(i);
os.seekp(0, std::ios_base::beg);
}
Timestamp end(nowTimestamp());
printf("benchStringStream %s\n", format("%S", (end - start)).c_str());
}
template<typename T>
void benchLogStream()
{
Timestamp start(nowTimestamp());
LogStream os;
for (size_t i = 0; i < N; ++i) {
os << (T)(i);
os.resetBuffer();
}
Timestamp end(nowTimestamp());
printf("benchLogStream %s\n", format("%S", (end - start)).c_str());
}
int main()
{
benchPrintf<int>("%d");
puts("int");
benchPrintf<int>("%d");
benchStringStream<int>();
benchLogStream<int>();
puts("double");
benchPrintf<double>("%.12g");
benchStringStream<double>();
benchLogStream<double>();
puts("int64_t");
benchPrintf<int64_t>("%" PRId64);
benchStringStream<int64_t>();
benchLogStream<int64_t>();
puts("void*");
benchPrintf<void*>("%p");
benchStringStream<void*>();
benchLogStream<void*>();
}
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{7DC0D84B-7F96-4BB9-9B8A-D86B7B031E61}</ProjectGuid>
<RootNamespace>testlogstream</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="test_logstream.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="test_logstream.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment