mirror of
https://github.com/KjellKod/g3log.git
synced 2025-04-17 23:23:48 +02:00

--HG-- rename : g2log/Build.cmake => Build.cmake rename : g2log/CMakeLists.txt => CMakeLists.txt rename : g2log/CPackLists.txt => CPackLists.txt rename : g2log/Dynamic.cmake => Dynamic.cmake rename : g2log/example/Example.cmake => example/Example.cmake rename : g2log/example/main_contract.cpp => example/main_contract.cpp rename : g2log/example/main_sigsegv.cpp => example/main_sigsegv.cpp rename : g2log/src/active.hpp => src/active.hpp rename : g2log/src/crashhandler.hpp => src/crashhandler.hpp rename : g2log/src/crashhandler_unix.cpp => src/crashhandler_unix.cpp rename : g2log/src/crashhandler_win.cpp => src/crashhandler_win.cpp rename : g2log/src/g2filesink.cpp => src/g2filesink.cpp rename : g2log/src/g2filesink.hpp => src/g2filesink.hpp rename : g2log/src/g2filesinkhelper.ipp => src/g2filesinkhelper.ipp rename : g2log/src/g2future.hpp => src/g2future.hpp rename : g2log/src/g2log.cpp => src/g2log.cpp rename : g2log/src/g2log.hpp => src/g2log.hpp rename : g2log/src/g2loglevels.cpp => src/g2loglevels.cpp rename : g2log/src/g2loglevels.hpp => src/g2loglevels.hpp rename : g2log/src/g2logmessage.cpp => src/g2logmessage.cpp rename : g2log/src/g2logmessage.hpp => src/g2logmessage.hpp rename : g2log/src/g2logmessagecapture.cpp => src/g2logmessagecapture.cpp rename : g2log/src/g2logmessagecapture.hpp => src/g2logmessagecapture.hpp rename : g2log/src/g2logworker.cpp => src/g2logworker.cpp rename : g2log/src/g2logworker.hpp => src/g2logworker.hpp rename : g2log/src/g2moveoncopy.hpp => src/g2moveoncopy.hpp rename : g2log/src/g2sink.hpp => src/g2sink.hpp rename : g2log/src/g2sinkhandle.hpp => src/g2sinkhandle.hpp rename : g2log/src/g2sinkwrapper.hpp => src/g2sinkwrapper.hpp rename : g2log/src/g2time.cpp => src/g2time.cpp rename : g2log/src/g2time.hpp => src/g2time.hpp rename : g2log/src/shared_queue.hpp => src/shared_queue.hpp rename : g2log/src/std2_make_unique.hpp => src/std2_make_unique.hpp rename : g2log/src/stlpatch_future.hpp => src/stlpatch_future.hpp rename : g2log/test_performance/Performance.cmake => test_performance/Performance.cmake rename : g2log/test_performance/main_threaded_mean.cpp => test_performance/main_threaded_mean.cpp rename : g2log/test_performance/main_threaded_worst.cpp => test_performance/main_threaded_worst.cpp rename : g2log/test_performance/performance.h => test_performance/performance.h rename : g2log/test_unit/Test.cmake => test_unit/Test.cmake rename : g2log/test_unit/test_concept_sink.cpp => test_unit/test_concept_sink.cpp rename : g2log/test_unit/test_configuration.cpp => test_unit/test_configuration.cpp rename : g2log/test_unit/test_filechange.cpp => test_unit/test_filechange.cpp rename : g2log/test_unit/test_io.cpp => test_unit/test_io.cpp rename : g2log/test_unit/test_linux_dynamic_loaded_sharedlib.cpp => test_unit/test_linux_dynamic_loaded_sharedlib.cpp rename : g2log/test_unit/test_sink.cpp => test_unit/test_sink.cpp rename : g2log/test_unit/tester_sharedlib.cpp => test_unit/tester_sharedlib.cpp rename : g2log/test_unit/tester_sharedlib.h => test_unit/tester_sharedlib.h rename : g2log/test_unit/testing_helpers.cpp => test_unit/testing_helpers.cpp rename : g2log/test_unit/testing_helpers.h => test_unit/testing_helpers.h
84 lines
3.1 KiB
C++
84 lines
3.1 KiB
C++
/** ==========================================================================
|
|
* 2012 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
|
|
* with no warranties. This code is yours to share, use and modify with no
|
|
* strings attached and no restrictions or obligations.
|
|
*
|
|
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
|
|
* ============================================================================
|
|
* Filename:g2time.cpp cross-platform, thread-safe replacement for C++11 non-thread-safe
|
|
* localtime (and similar)
|
|
* Created: 2012 by Kjell Hedström
|
|
*
|
|
* PUBLIC DOMAIN and Not under copywrite protection. First published for g2log at KjellKod.cc
|
|
* ********************************************* */
|
|
|
|
#include "g2time.hpp"
|
|
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <chrono>
|
|
#include <thread>
|
|
#include <cassert>
|
|
#include <iomanip>
|
|
|
|
|
|
namespace g2 {
|
|
namespace internal {
|
|
// This mimics the original "std::put_time(const std::tm* tmb, const charT* fmt)"
|
|
// This is needed since latest version (at time of writing) of gcc4.7 does not implement this library function yet.
|
|
// return value is SIMPLIFIED to only return a std::string
|
|
|
|
std::string put_time(const struct tm* tmb, const char* c_time_format) {
|
|
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) && !defined(__MINGW32__)
|
|
std::ostringstream oss;
|
|
oss.fill('0');
|
|
// BOGUS hack done for VS2012: C++11 non-conformant since it SHOULD take a "const struct tm* "
|
|
oss << std::put_time(const_cast<struct tm*> (tmb), c_time_format);
|
|
return oss.str();
|
|
#else // LINUX
|
|
const size_t size = 1024;
|
|
char buffer[size]; // IMPORTANT: check now and then for when gcc will implement std::put_time.
|
|
// ... also ... This is way more buffer space then we need
|
|
|
|
auto success = std::strftime(buffer, size, c_time_format, tmb);
|
|
if (0 == success)
|
|
{
|
|
assert((0 != success) && "strftime fails with illegal formatting");
|
|
return c_time_format;
|
|
}
|
|
|
|
return buffer;
|
|
#endif
|
|
}
|
|
} // internal
|
|
} // g2
|
|
|
|
|
|
|
|
namespace g2 {
|
|
|
|
std::time_t systemtime_now() {
|
|
system_time_point system_now = std::chrono::system_clock::now();
|
|
return std::chrono::system_clock::to_time_t(system_now);
|
|
}
|
|
|
|
tm localtime(const std::time_t& time) {
|
|
struct tm tm_snapshot;
|
|
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__) && !defined(__GNUC__))
|
|
localtime_s(&tm_snapshot, &time); // windsows
|
|
#else
|
|
localtime_r(&time, &tm_snapshot); // POSIX
|
|
#endif
|
|
return tm_snapshot;
|
|
}
|
|
|
|
/// returns a std::string with content of time_t as localtime formatted by input format string
|
|
/// * format string must conform to std::put_time
|
|
/// This is similar to std::put_time(std::localtime(std::time_t*), time_format.c_str());
|
|
|
|
std::string localtime_formatted(const std::time_t& time_snapshot, const std::string& time_format) {
|
|
std::tm t = localtime(time_snapshot); // could be const, but cannot due to VS2012 is non conformant for C++11's std::put_time (see above)
|
|
return g2::internal::put_time(&t, time_format.c_str()); // format example: //"%Y/%m/%d %H:%M:%S");
|
|
}
|
|
} // g2
|