diff options
-rw-r--r-- | retrace/daemon/glframe_logger.cpp | 35 | ||||
-rw-r--r-- | retrace/daemon/glframe_logger.hpp | 4 | ||||
-rw-r--r-- | retrace/daemon/glframe_os.hpp | 5 | ||||
-rw-r--r-- | retrace/daemon/glframe_os_linux.cpp | 6 |
4 files changed, 30 insertions, 20 deletions
diff --git a/retrace/daemon/glframe_logger.cpp b/retrace/daemon/glframe_logger.cpp index 4fc8ca10..94431d80 100644 --- a/retrace/daemon/glframe_logger.cpp +++ b/retrace/daemon/glframe_logger.cpp @@ -30,9 +30,8 @@ #include <assert.h> #include <fcntl.h> #include <sys/stat.h> -#include <unistd.h> +#include <time.h> -#include <sys/time.h> #include <sstream> #include <string> @@ -68,17 +67,17 @@ Logger::Logger(const std::string &out_directory) : Thread("logger"), m_running(true) { std::stringstream ss; ss << out_directory << "/frame_retrace.log"; - mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; - m_fh = open(ss.str().c_str(), - O_WRONLY | O_APPEND | O_CREAT | O_SYNC, - mode); - assert(m_fh > -1); - m_read_fh = open(ss.str().c_str(), O_RDONLY); - assert(m_read_fh > -1); + m_fh = fopen(ss.str().c_str(), "a"); + assert(m_fh != NULL); + m_read_fh = fopen(ss.str().c_str(), "r"); + assert(m_read_fh != NULL); } Logger::~Logger() { - close(m_fh); + fclose(m_fh); + m_fh = NULL; + fclose(m_read_fh); + m_read_fh = NULL; } const char * @@ -103,13 +102,13 @@ Logger::Log(Severity s, const std::string &file, int line, assert(m_instance); if (s < m_instance->m_severity) return; - timeval current_time; - gettimeofday(¤t_time, NULL); - int milli = current_time.tv_usec / 1000; + time_t t = time(NULL); char ts[255]; - strftime(ts, 255, "%Y %b %d %H:%M:%S.", localtime(¤t_time.tv_sec)); + struct tm result; + glretrace_localtime(&t, &result); + strftime(ts, 255, "%Y %b %d %H:%M:%S", &result); std::stringstream ss; - ss << "[" << ts << milli << "]" + ss << "[" << ts << "] " << file << " " << line << " (" << format_severity(s) << "): " << message << "\n"; @@ -136,8 +135,10 @@ Logger::Flush() { front = m_instance->m_q.front(); m_instance->m_q.pop(); } - write(m_instance->m_fh, front.c_str(), front.size()); + fwrite(front.c_str(), 1, front.length(), m_instance->m_fh); } + ScopedLock sl(m_instance->m_protect); + fflush(m_instance->m_fh); } void @@ -163,7 +164,7 @@ Logger::GetLog(std::string *out) { char buf[1024]; size_t bytes = 1; while (bytes > 0) { - bytes = read(m_instance->m_read_fh, buf, 1023); + bytes = fread(buf, 1, 1023, m_instance->m_read_fh); buf[bytes] = '\0'; out->append(buf); } diff --git a/retrace/daemon/glframe_logger.hpp b/retrace/daemon/glframe_logger.hpp index 3bf56040..b67b8b91 100644 --- a/retrace/daemon/glframe_logger.hpp +++ b/retrace/daemon/glframe_logger.hpp @@ -29,6 +29,7 @@ #define OS_GFLOGGER_H_ #include <stdarg.h> +#include <stdio.h> #include <mutex> #include <queue> @@ -67,8 +68,7 @@ class Logger : public Thread { std::queue<std::string> m_q; std::mutex m_protect; Semaphore m_sem; - int m_fh; - int m_read_fh; + FILE *m_fh, *m_read_fh; int m_severity; bool m_running; }; diff --git a/retrace/daemon/glframe_os.hpp b/retrace/daemon/glframe_os.hpp index 6bad244e..018e4139 100644 --- a/retrace/daemon/glframe_os.hpp +++ b/retrace/daemon/glframe_os.hpp @@ -28,9 +28,10 @@ #ifndef _GLFRAME_OS_H_ #define _GLFRAME_OS_H_ +#include <time.h> + #include <mutex> #include <condition_variable> -// #include <pthread.h> #include "glframe_traits.hpp" @@ -62,6 +63,8 @@ class Semaphore : NoCopy, NoAssign, NoMove { int fork_execv(const char *path, const char *const argv[]); +struct tm *glretrace_localtime(const time_t *timep, struct tm *result); + } // namespace glretrace #endif // _GLFRAME_OS_H_ diff --git a/retrace/daemon/glframe_os_linux.cpp b/retrace/daemon/glframe_os_linux.cpp index 0d4ae1db..5fcbd2c4 100644 --- a/retrace/daemon/glframe_os_linux.cpp +++ b/retrace/daemon/glframe_os_linux.cpp @@ -29,6 +29,8 @@ #include <stdio.h> #include <stdlib.h> #include <unistd.h> +#include <sys/time.h> +#include <time.h> namespace glretrace { @@ -46,4 +48,8 @@ int fork_execv(const char *path, const char *const argv[]) { return 0; } +struct tm *glretrace_localtime(const time_t *timep, struct tm *result) { + return localtime_r(timep, result); +} + } // namespace glretrace |