Fix launchpad log to rotate correctly 98/324098/2
authorJihoi Kim <jihoi.kim@samsung.com>
Tue, 13 May 2025 04:57:29 +0000 (13:57 +0900)
committerJihoi Kim <jihoi.kim@samsung.com>
Tue, 13 May 2025 05:05:52 +0000 (14:05 +0900)
- Use std::ios::ate instead of std::ios::app
- Make index be initialized into 0 when log file reach threshold
- Add single character flag to indicate border of log

Change-Id: I242b721bc2e7e582bc51564343801f450d06ba69
Signed-off-by: Jihoi Kim <jihoi.kim@samsung.com>
src/launchpad-process-pool/logger.cc

index 137fc2a40085083601e12fd95e0fae8ff8028285..1ab21c581682d673916fb538fa14265befb837e7 100644 (file)
@@ -86,7 +86,7 @@ Logger::Logger(std::string path) : path_(std::move(path)) {
 
   if (CreateLaunchpadDirectories() != 0) THROW(-EIO);
 
-  stream_.open(path_, std::ios::out | std::ios::app);
+  stream_.open(path_, std::ios::out | std::ios::ate);
   if (!stream_.is_open()) {
     _E("Failed to open path(%s)", path_.c_str());
     THROW(-EIO);
@@ -120,20 +120,25 @@ void Logger::Print(const char *tag, const char *format, ...) {
   struct tm tm;
   localtime_r(&t, &tm);
 
+  static char border = ' ';
   const int threshold = 1000000;  // 1MB
-  if (stream_.tellp() >= threshold) stream_.seekp(0);
+  if (stream_.tellp() >= threshold) {
+    stream_.seekp(0);
+    border = border == ' ' ? '+' : ' ';
+    index_ = 0;
+  }
 
   char buf[256];
   int len = snprintf(buf, sizeof(buf),
-                     "[%6d] %04d-%02d-%02d %02d:%02d:%02d %-16s %-100s\n",
-                     index_, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
-                     tm.tm_hour, tm.tm_min, tm.tm_sec, tag, format_buf);
+                     "[%c%5d] %04d-%02d-%02d %02d:%02d:%02d %-16s %-100s\n",
+                     border, index_++, tm.tm_year + 1900, tm.tm_mon + 1,
+                     tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
+                     tag, format_buf);
 
   stream_.write(buf, len);
   if (!stream_.good()) _E("write() is failed");
 
   stream_.flush();
-  index_ = (index_ + 1) % 2500;
 }
 
 }  // namespace launchpad