Implementing log rotation for Cef

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Implementing log rotation for Cef

Postby octavian » Tue Sep 04, 2018 8:51 am

I start Cef and initialize it using a certain log file, say /tmp/mylog.txt
At some point in time, based on some triggered event, I have code that gets invoked & it renames the file using rename(): http://www.cplusplus.com/reference/cstdio/rename/
which basically renames /tmp/mylog.txt into /tmp/mylog-1.txt
followed by the creation of an empty new file:
FILE* pFile;
pFile = fopen("/tmp/mylog.txt", "w");
if (pFile != NULL) {
fclose(pFile);
}

This goes well , except that Cef is not aware and it continues to write to the original file, even though it has been renamed to: /tmp/mylog-1.txt
So it continues to write to /tmp/mylog-1.txt while /tmp/mylog.txt says empty!

Is there a way to reset the logging on Cef so that it writes to the newly created log file / (i.e. the same file name as it used to write to, i.e. /tmp/mylog.txt)
I don't want to shutdown Cef just for this file rotation implementation..

Thank you!
octavian
Techie
 
Posts: 36
Joined: Thu Jul 19, 2018 9:35 am

Re: Implementing log rotation for Cef

Postby Czarek » Tue Sep 04, 2018 9:10 am

You can rotate the file before initializing CEF. There is Issue #2154 to improve CEF logging: https://bitbucket.org/chromiumembedded/ ... -functions
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Implementing log rotation for Cef

Postby octavian » Tue Sep 04, 2018 9:13 am

I should mention that the way we initialize Cef using the log file is this:

#define INI_CONF_LOGPATH "LogPath"
#define MAX_LOG_ENTRY_LENGTH 150

char log_path[MAX_LOG_ENTRY_LENGTH];
char conf_section[10] = {"test123"};
char conf_log_path[10] = {"LogPath"};
char conf_log_default[100] = {"/tmp/mylog.txt"};

miGetPrivateProfileString(conf_section, conf_log_path, conf_log_default, log_path, MAX_LOG_ENTRY_LENGTH);
CefString(&settings.log_file).FromString(std::string(log_path));
settings.log_severity = cef_log_severity_t::LOGSEVERITY_VERBOSE;

bool rc = CefInitialize(main_args, settings, app.get(), NULL);
octavian
Techie
 
Posts: 36
Joined: Thu Jul 19, 2018 9:35 am

Re: Implementing log rotation for Cef

Postby octavian » Tue Sep 04, 2018 9:23 am

@Czarek - the work around to do file rotation BEFORE CefInitialize() won't work for me.

CefInitialize() is done once and then logging starts (i.e. within a host session).
Cef runs as long as the host session is active which may stay for days.
During this time there is potential that log file can grow very big.
I want to be able to back up current log file to some other file name *WHILE* Cef is running, thus overwriting previous/older one (if it was present), this way I always ensure I keep maximum 2 log files.

You pointed to a link with some issues.. Does it mean currently Cef does not support this scenario I'm describing ?

Thank you!
Last edited by octavian on Tue Sep 04, 2018 11:01 am, edited 1 time in total.
octavian
Techie
 
Posts: 36
Joined: Thu Jul 19, 2018 9:35 am

Re: Implementing log rotation for Cef

Postby magreenblatt » Tue Sep 04, 2018 9:33 am

It is not supported by CEF. You might want to research third-party tools that support log rotation.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Implementing log rotation for Cef

Postby octavian » Wed Sep 05, 2018 9:34 am

Now that I think about.. How would third party tools solve the issue since it's Cef that keeps a hold on that file handle even after renaming the file ?
Third party tools will indeed take care of log renaming , deletion, etc, but if Cef (once it's initialized) keeps writing to same file and it does not switch to dump to a new file, then issue is not solved.

So my current observation on Cef is this:
- initialize Cef by specifying a given file name, say /tmp/log.txt
- at some point, functionality is triggered and renames that file to /tmp/log-1.txt AND at the same time it creates a new file (with 'w' write property) using original name: /tmp/log.txt
- Cef keeps writing to /tmp/log-1.txt instead of the newly created file: /tmp/log.txt
octavian
Techie
 
Posts: 36
Joined: Thu Jul 19, 2018 9:35 am

Re: Implementing log rotation for Cef

Postby magreenblatt » Wed Sep 05, 2018 9:50 am

Thanks for doing the test. It sounds like you'll need to restart the application to rotate the logs. For example, ChromeOS rotates logs at login time. If you prefer a different approach you'll need to implement it yourself in Chromium.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: Implementing log rotation for Cef

Postby HarmlessDave » Wed Sep 05, 2018 12:35 pm

You could try copying the file then truncating it. Log.txt > log-old; set log.txt length 0.

We use our own logging framework so I haven't tried this. You'd want exception handling in case there's a race condition like truncating during a message write.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Implementing log rotation for Cef

Postby octavian » Wed Sep 05, 2018 2:56 pm

It looks like it worked (i.e. I can run: cp -f <src> <target>, to force copy , so that in case target exists , it does not prompt
& I can truncate current log file).

THANK YOU !!!!!
octavian
Techie
 
Posts: 36
Joined: Thu Jul 19, 2018 9:35 am


Return to Support Forum

Who is online

Users browsing this forum: Majestic-12 [Bot] and 60 guests