Replace timeval junk with std::chrono

This commit is contained in:
Thomas Goyne 2014-05-25 18:16:28 -07:00
parent 650cfcb043
commit 1f7c47239b
10 changed files with 33 additions and 142 deletions

View File

@ -80,7 +80,6 @@
<ClInclude Include="$(SrcDir)include\libaegisub\spellchecker.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\split.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\thesaurus.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\time.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\util_osx.h" />
<ClInclude Include="$(SrcDir)include\libaegisub\vfr.h" />

View File

@ -152,9 +152,6 @@
<ClInclude Include="$(SrcDir)include\libaegisub\split.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\time.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(SrcDir)include\libaegisub\ass\uuencode.h">
<Filter>ASS</Filter>
</ClInclude>
@ -334,4 +331,4 @@
<Filter>Header Files</Filter>
</None>
</ItemGroup>
</Project>
</Project>

View File

@ -17,16 +17,15 @@
#include "libaegisub/cajun/elements.h"
#include "libaegisub/cajun/writer.h"
#include "libaegisub/dispatch.h"
#include "libaegisub/time.h"
#include "libaegisub/util.h"
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/range/algorithm/remove_if.hpp>
#include <chrono>
namespace agi {
namespace log {
namespace agi { namespace log {
/// Global log sink.
LogSink *log;
@ -75,12 +74,13 @@ decltype(LogSink::messages) LogSink::GetMessages() const {
Message::Message(const char *section, Severity severity, const char *file, const char *func, int line)
: msg(buffer, sizeof buffer)
{
using namespace std::chrono;
sm.section = section;
sm.severity = severity;
sm.file = file;
sm.func = func;
sm.line = line;
sm.tv = util::time_log();
sm.time = duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
}
Message::~Message() {
@ -91,17 +91,12 @@ Message::~Message() {
JsonEmitter::JsonEmitter(fs::path const& directory)
: fp(new boost::filesystem::ofstream(unique_path(directory/util::strftime("%Y-%m-%d-%H-%M-%S-%%%%%%%%.json"))))
{
WriteTime("open");
}
JsonEmitter::~JsonEmitter() {
WriteTime("close");
}
void JsonEmitter::log(SinkMessage *sm) {
json::Object entry;
entry["sec"] = (int64_t)sm->tv.tv_sec;
entry["usec"] = (int64_t)sm->tv.tv_usec;
entry["sec"] = sm->time / 1000000000;
entry["usec"] = sm->time % 1000000000;
entry["severity"] = sm->severity;
entry["section"] = sm->section;
entry["file"] = sm->file;
@ -112,14 +107,4 @@ void JsonEmitter::log(SinkMessage *sm) {
fp->flush();
}
void JsonEmitter::WriteTime(const char *key) {
auto time = util::time_log();
json::Object obj;
json::Array &timeval = obj[key];
timeval.push_back((int64_t)time.tv_sec);
timeval.push_back((int64_t)time.tv_usec);
json::Writer::Write(obj, *fp);
}
} // namespace log
} // namespace agi
} }

View File

@ -12,16 +12,11 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// @file log.h
/// @brief Logging
/// @ingroup libaegisub
#include <libaegisub/fs_fwd.h>
#include <libaegisub/time.h>
#include <boost/circular_buffer.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <iosfwd>
#include <cstdint>
#include <memory>
#include <vector>
@ -39,18 +34,18 @@
#define LOG_D_IF(cond, section) if (cond) LOG_SINK(section, agi::log::Debug)
namespace agi {
namespace dispatch { class Queue; }
namespace dispatch { class Queue; }
namespace log {
namespace log {
class LogSink;
/// Severity levels
enum Severity {
Exception, ///< Used when exceptions are thrown
Assert, ///< Fatal and non-fatal assert logging
Warning, ///< Warnings
Info, ///< Information only
Debug ///< Enabled by default when compiled in debug mode.
Exception, ///< Used when exceptions are thrown
Assert, ///< Fatal and non-fatal assert logging
Warning, ///< Warnings
Info, ///< Information only
Debug ///< Enabled by default when compiled in debug mode.
};
/// Short Severity ID
@ -62,13 +57,13 @@ extern LogSink *log;
/// Container to hold a single message
struct SinkMessage {
std::string message; ///< Formatted message
agi_timeval tv; ///< Time at execution
const char *section; ///< Section info eg "video/open" "video/seek" etc
const char *file; ///< Source file
const char *func; ///< Function name
Severity severity; ///< Severity
int line; ///< Source line
std::string message; ///< Formatted message
int64_t time; ///< Time at execution in nanoseconds since epoch
const char *section; ///< Section info eg "video/open" "video/seek" etc
const char *file; ///< Source file
const char *func; ///< Function name
Severity severity; ///< Severity
int line; ///< Source line
};
class Emitter;
@ -115,14 +110,10 @@ public:
class JsonEmitter final : public Emitter {
std::unique_ptr<std::ostream> fp;
void WriteTime(const char *key);
public:
/// Constructor
/// @param directory Directory to write the log file in
JsonEmitter(fs::path const& directory);
/// Destructor
~JsonEmitter();
void log(SinkMessage *) override;
};

View File

@ -1,34 +0,0 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#pragma once
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#else
# include <ctime>
#endif
#ifdef _WIN32
// timeval on windows is defined by winsock, which is a bit much to drag in for
// a pair of longs
struct agi_timeval {
long tv_sec;
long tv_usec;
};
#else
typedef timeval agi_timeval;
#endif

View File

@ -12,8 +12,6 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <libaegisub/time.h>
#include <algorithm>
#include <boost/range/irange.hpp>
#include <string>
@ -25,9 +23,6 @@ namespace agi {
/// Clamp `b` to the range [`a`,`c`]
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
/// Get time suitable for logging mechanisms.
agi_timeval time_log();
bool try_parse(std::string const& str, double *out);
bool try_parse(std::string const& str, int *out);

View File

@ -12,30 +12,31 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "libaegisub/log.h"
#include <cstdio>
#include <ctime>
#include <unistd.h>
#include "libaegisub/log.h"
namespace agi { namespace log {
void EmitSTDOUT::log(SinkMessage *sm) {
time_t time = sm->time / 1000000000;
tm tmtime;
localtime_r(&sm->tv.tv_sec, &tmtime);
localtime_r(&time, &tmtime);
printf("%c %02d:%02d:%02d %-6ld <%-25s> [%s:%s:%d] %.*s\n",
Severity_ID[sm->severity],
tmtime.tm_hour,
tmtime.tm_min,
tmtime.tm_sec,
(long)sm->tv.tv_usec,
(long)(sm->time % 1000000000),
sm->section,
sm->file,
sm->func,
sm->line,
(int)sm->message.size(),
sm->message.c_str());
if (!isatty(fileno(stdout)))
fflush(stdout);
}

View File

@ -23,13 +23,6 @@
#endif
namespace agi { namespace util {
timeval time_log() {
timeval tv;
gettimeofday(&tv, nullptr);
return tv;
}
void SetThreadName(const char *) { }
void sleep_for(int ms) {

View File

@ -12,10 +12,6 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// @file util.cpp
/// @brief Windows utility methods.
/// @ingroup libaegisub windows
#include "libaegisub/util.h"
#include "libaegisub/charset_conv_win.h"
@ -43,39 +39,6 @@ std::string ErrorString(int error) {
return str;
}
/// @brief Get seconds and microseconds.
/// @param tv[out] agi_timeval struct
/// This code is from http://www.suacommunity.com/dictionary/gettimeofday-entry.php
agi_timeval time_log() {
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
// Define a structure to receive the current Windows filetime
FILETIME ft;
// Initialize the present time to 0 and the timezone to UTC
unsigned __int64 tmpres = 0;
GetSystemTimeAsFileTime(&ft);
// The GetSystemTimeAsFileTime returns the number of 100 nanosecond
// intervals since Jan 1, 1601 in a structure. Copy the high bits to
// the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
// Convert to microseconds by dividing by 10
tmpres /= 10;
// The Unix epoch starts on Jan 1 1970. Need to subtract the difference
// in seconds from Jan 1 1601.
tmpres -= DELTA_EPOCH_IN_MICROSECS;
// Finally change microseconds to seconds and place in the seconds value.
// The modulus picks up the microseconds.
agi_timeval tv = { (long)(tmpres / 1000000UL), (long)(tmpres % 1000000UL) };
return tv;
}
#define MS_VC_EXCEPTION 0x406d1388
/// Parameters for setting the thread name

View File

@ -53,15 +53,16 @@ public:
}
void log(agi::log::SinkMessage *sm) override {
time_t time = sm->time / 1000000000;
#ifndef _WIN32
tm tmtime;
localtime_r(&sm->tv.tv_sec, &tmtime);
localtime_r(&time, &tmtime);
auto log = wxString::Format("%c %02d:%02d:%02d %-6ld <%-25s> [%s:%s:%d] %s\n",
agi::log::Severity_ID[sm->severity],
(int)tmtime.tm_hour,
(int)tmtime.tm_min,
(int)tmtime.tm_sec,
(long)sm->tv.tv_usec,
(long)(sm->time % 1000000000),
sm->section,
sm->file,
sm->func,
@ -70,7 +71,7 @@ public:
#else
auto log = wxString::Format("%c %-6ld <%-25s> [%s:%s:%d] %s\n",
agi::log::Severity_ID[sm->severity],
sm->tv.tv_usec,
(long)(sm->time % 1000000000),
sm->section,
sm->file,
sm->func,