mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-31 23:24:20 +03:00 
			
		
		
		
	upm logger using nanoseconds only timestamp
Signed-off-by: Mihai Stefanescu <mihai.t.gh.stefanescu@gmail.com>
This commit is contained in:
		 Mihai Stefanescu
					Mihai Stefanescu
				
			
				
					committed by
					
						 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
			
				
	
			
			
			 Stefan Andritoiu
						Stefan Andritoiu
					
				
			
						parent
						
							c23d076a1b
						
					
				
				
					commit
					422b6926b5
				
			| @@ -32,12 +32,12 @@ | |||||||
| #include <thread> | #include <thread> | ||||||
|  |  | ||||||
| /* Helper macro for logger utility class. */ | /* Helper macro for logger utility class. */ | ||||||
| #define UPM_LOG(log_level)                                                                     \ | #define UPM_LOG(log_level)                                                                       \ | ||||||
|     if (log_level < UPM_LOGGER::LogLevel())                                                    \ |     if (log_level < UPM_LOGGER::LogLevel())                                                      \ | ||||||
|         ;                                                                                      \ |         ;                                                                                        \ | ||||||
|     else                                                                                       \ |     else                                                                                         \ | ||||||
|     UPM_LOGGER::getInstance().log(log_level) << LogHelper() << __FILE__ << " " << __FUNCTION__ \ |     (LogHelper() && UPM_LOGGER::getInstance()).log(log_level) << __FILE__ << " " << __FUNCTION__ \ | ||||||
|                                              << " " << __LINE__ << ": " |                                                               << " " << __LINE__ << ": " | ||||||
|  |  | ||||||
| #define DEFAULT_LOG_FILE "/dev/stdout" | #define DEFAULT_LOG_FILE "/dev/stdout" | ||||||
|  |  | ||||||
| @@ -69,8 +69,6 @@ class UPM_LOGGER | |||||||
|     { |     { | ||||||
|         using namespace std::chrono; |         using namespace std::chrono; | ||||||
|  |  | ||||||
|         std::lock_guard<std::mutex> lock(logMutex()); |  | ||||||
|  |  | ||||||
|         _logStream << "UPM - " |         _logStream << "UPM - " | ||||||
|                    << duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count(); |                    << duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count(); | ||||||
|         _logStream << " [" << getLogLevelName(level) << "] "; |         _logStream << " [" << getLogLevelName(level) << "] "; | ||||||
| @@ -103,6 +101,7 @@ class UPM_LOGGER | |||||||
|         return file; |         return file; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|   private: |   private: | ||||||
|     UPM_LOGGER() |     UPM_LOGGER() | ||||||
|     { |     { | ||||||
| @@ -124,25 +123,27 @@ class UPM_LOGGER | |||||||
|     std::ofstream _logStream; |     std::ofstream _logStream; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | /* This class exists only to append a newline when the log statement ends.*/ | ||||||
| class LogHelper | class LogHelper | ||||||
| { | { | ||||||
|   public: |   public: | ||||||
|     LogHelper() |     LogHelper() | ||||||
|     { |     { | ||||||
|         _os = &UPM_LOGGER::getInstance()._logStream; |  | ||||||
|         _mutex = &UPM_LOGGER::logMutex(); |         _mutex = &UPM_LOGGER::logMutex(); | ||||||
|  |         _mutex->lock(); | ||||||
|  |         _os = &UPM_LOGGER::getInstance()._logStream; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     ~LogHelper() |     ~LogHelper() | ||||||
|     { |     { | ||||||
|         std::lock_guard<std::mutex> lock(*_mutex); |  | ||||||
|         *(_os) << std::endl; |         *(_os) << std::endl; | ||||||
|  |         _mutex->unlock(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     friend std::ofstream& |     friend UPM_LOGGER& | ||||||
|     operator<<(std::ofstream& os, const LogHelper& h) |     operator&&(const LogHelper& l, UPM_LOGGER& r) | ||||||
|     { |     { | ||||||
|         return os; |         return r; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   private: |   private: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user