log rotation in libosmocore?

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/OpenBSC@lists.osmocom.org/.

Neels Hofmeyr nhofmeyr at sysmocom.de
Wed Apr 4 13:10:18 UTC 2018


I client of ours has the desire to merge a patch to libosmocore that adds
simplistic log rotation functionality.

Our first response was against it: we have the SIGHUP handling that allows
logrotate to tell osmocom programs to re-open their log file(s).

However, the system in question is based on BusyBox, which lacks logrotate. I
haven't yet investigated why it can't just be installed, but there seems to be
some or other issue about that.

The next best idea is to cook up a shell script that does more or less what
logrotate does ... and probably run into problems that logrotate already
solves better. The script needs to clean old files and invoke SIGHUP...

So I'd like to ask around, maybe it is acceptable to merge a poor-man's log
rotation functionality to libosmocore after all?

The patch needs some improvements (VTY configurability for starters), but so
far goes something like this:


 static void _file_output(struct log_target *target, unsigned int level,
 			 const char *log)
 {
-	fprintf(target->tgt_file.out, "%s", log);
-	fflush(target->tgt_file.out);
+	int n;
+
+	if (target->tgt_file.out) {
+
+		n = fprintf(target->tgt_file.out, "%s", log);
+		fflush(target->tgt_file.out);
+#ifdef stderr
+		/* don't try try to roll stderr */
+		if (target->tgt_file.out != stderr)
+#endif
+		{
+			if (n > 0) {
+				target->tgt_file.written_count += n;
+
+				if (target->tgt_file.roll_count != 0 &&
+					target->tgt_file.written_count > target->tgt_file.roll_count) {
+
+					/* Create filename */
+					char rotname[strlen(target->tgt_file.fname) + 3];
+
+					strcpy(rotname, target->tgt_file.fname);
+					strcat(rotname, ".1");
+
+					/* Rename the current log, then reopen the log to start new file */
+					rename(target->tgt_file.fname, rotname);
+					log_target_file_reopen(target);
+				}
+			}
+		}
+	}
 }


Note, this follows a simplistic approach to removing old log files: there is
always exactly one rotated-away file, 'mylogfile.1', which gets overwritten in
each rotation.

Any rotation features more elaborate than this should better be solved with
logrotate, but would everyone accept this kind of patch merged to libosmocore?

Thanks,

~N
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.osmocom.org/pipermail/openbsc/attachments/20180404/8c97597c/attachment.bin>


More information about the OpenBSC mailing list