mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 16:35:45 +08:00
cleaned common.h and moved some code around
This commit is contained in:
parent
10d5292bbb
commit
80a432e646
@ -5,7 +5,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -19,7 +18,6 @@
|
|||||||
|
|
||||||
#include <spdlog/details/null_mutex.h>
|
#include <spdlog/details/null_mutex.h>
|
||||||
|
|
||||||
|
|
||||||
//visual studio upto 2013 does not support noexcept nor constexpr
|
//visual studio upto 2013 does not support noexcept nor constexpr
|
||||||
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
#if defined(_MSC_VER) && (_MSC_VER < 1900)
|
||||||
#define SPDLOG_NOEXCEPT throw()
|
#define SPDLOG_NOEXCEPT throw()
|
||||||
@ -30,7 +28,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -41,7 +38,6 @@ namespace sinks
|
|||||||
class sink;
|
class sink;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common types across the lib
|
|
||||||
using log_clock = std::chrono::system_clock;
|
using log_clock = std::chrono::system_clock;
|
||||||
using sink_ptr = std::shared_ptr < sinks::sink >;
|
using sink_ptr = std::shared_ptr < sinks::sink >;
|
||||||
using sinks_init_list = std::initializer_list < sink_ptr >;
|
using sinks_init_list = std::initializer_list < sink_ptr >;
|
||||||
@ -115,21 +111,10 @@ private:
|
|||||||
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
||||||
//
|
//
|
||||||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
#define SPDLOG_FILENAME_T(s) L ## s
|
|
||||||
using filename_t = std::wstring;
|
using filename_t = std::wstring;
|
||||||
inline std::string filename_to_str(const filename_t& filename)
|
|
||||||
{
|
|
||||||
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
|
|
||||||
return c.to_bytes(filename);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
#define SPDLOG_FILENAME_T(s) s
|
|
||||||
using filename_t = std::string;
|
using filename_t = std::string;
|
||||||
|
|
||||||
inline std::string filename_to_str(const filename_t& filename)
|
|
||||||
{
|
|
||||||
return filename;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
} //spdlog
|
} //spdlog
|
||||||
|
@ -25,6 +25,7 @@ namespace details
|
|||||||
|
|
||||||
class file_helper
|
class file_helper
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const int open_tries = 5;
|
const int open_tries = 5;
|
||||||
const int open_interval = 10;
|
const int open_interval = 10;
|
||||||
@ -57,7 +58,7 @@ public:
|
|||||||
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
|
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
|
||||||
}
|
}
|
||||||
|
|
||||||
throw spdlog_ex("Failed opening file " + filename_to_str(_filename) + " for writing");
|
throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing");
|
||||||
}
|
}
|
||||||
|
|
||||||
void reopen(bool truncate)
|
void reopen(bool truncate)
|
||||||
@ -88,34 +89,30 @@ public:
|
|||||||
size_t msg_size = msg.formatted.size();
|
size_t msg_size = msg.formatted.size();
|
||||||
auto data = msg.formatted.data();
|
auto data = msg.formatted.data();
|
||||||
if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
|
if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
|
||||||
throw spdlog_ex("Failed writing to file " + filename_to_str(_filename));
|
throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename));
|
||||||
|
|
||||||
if (_force_flush)
|
if (_force_flush)
|
||||||
std::fflush(_fd);
|
std::fflush(_fd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long size()
|
long size()
|
||||||
{
|
{
|
||||||
if (!_fd)
|
if (!_fd)
|
||||||
throw spdlog_ex("Cannot use size() on closed file " + filename_to_str(_filename));
|
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
|
||||||
|
|
||||||
auto pos = ftell(_fd);
|
auto pos = ftell(_fd);
|
||||||
if (fseek(_fd, 0, SEEK_END) != 0)
|
if (fseek(_fd, 0, SEEK_END) != 0)
|
||||||
throw spdlog_ex("fseek failed on file " + filename_to_str(_filename));
|
throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename));
|
||||||
|
|
||||||
auto file_size = ftell(_fd);
|
auto file_size = ftell(_fd);
|
||||||
|
|
||||||
if(fseek(_fd, pos, SEEK_SET) !=0)
|
if(fseek(_fd, pos, SEEK_SET) !=0)
|
||||||
throw spdlog_ex("fseek failed on file " + filename_to_str(_filename));
|
throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename));
|
||||||
|
|
||||||
if (file_size == -1)
|
if (file_size == -1)
|
||||||
throw spdlog_ex("ftell failed on file " + filename_to_str(_filename));
|
throw spdlog_ex("ftell failed on file " + os::filename_to_str(_filename));
|
||||||
|
|
||||||
|
|
||||||
return file_size;
|
return file_size;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const filename_t& filename() const
|
const filename_t& filename() const
|
||||||
@ -129,14 +126,10 @@ public:
|
|||||||
return os::file_exists(name);
|
return os::file_exists(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FILE* _fd;
|
FILE* _fd;
|
||||||
filename_t _filename;
|
filename_t _filename;
|
||||||
bool _force_flush;
|
bool _force_flush;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
|
|||||||
return !(tm1 == tm2);
|
return !(tm1 == tm2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// eol at end of each log line
|
// eol definition
|
||||||
#if !defined (SPDLOG_EOL)
|
#if !defined (SPDLOG_EOL)
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define SPDLOG_EOL "\r\n"
|
#define SPDLOG_EOL "\r\n"
|
||||||
@ -125,6 +125,7 @@ SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL;
|
|||||||
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
|
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//fopen_s on non windows for writing
|
//fopen_s on non windows for writing
|
||||||
inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode)
|
inline int fopen_s(FILE** fp, const filename_t& filename, const filename_t& mode)
|
||||||
{
|
{
|
||||||
@ -228,6 +229,23 @@ inline size_t thread_id()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
|
||||||
|
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
|
||||||
|
#define SPDLOG_FILENAME_T(s) L ## s
|
||||||
|
inline std::string filename_to_str(const filename_t& filename)
|
||||||
|
{
|
||||||
|
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> c;
|
||||||
|
return c.to_bytes(filename);
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
#define SPDLOG_FILENAME_T(s) s
|
||||||
|
inline std::string filename_to_str(const filename_t& filename)
|
||||||
|
{
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
} //os
|
} //os
|
||||||
} //details
|
} //details
|
||||||
} //spdlog
|
} //spdlog
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Trivial file sink with single file as target
|
* Trivial file sink with single file as target
|
||||||
*/
|
*/
|
||||||
@ -108,6 +108,7 @@ private:
|
|||||||
|
|
||||||
void _rotate()
|
void _rotate()
|
||||||
{
|
{
|
||||||
|
using details::os::filename_to_str;
|
||||||
_file_helper.close();
|
_file_helper.close();
|
||||||
for (auto i = _max_files; i > 0; --i)
|
for (auto i = _max_files; i > 0; --i)
|
||||||
{
|
{
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
|
|
||||||
// Return an existing logger or nullptr if a logger with such name doesn't exist.
|
// Return an existing logger or nullptr if a logger with such name doesn't exist.
|
||||||
// Examples:
|
// Examples:
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user