* -f option added Feb, 2008. This causes rotatelog to open/create
* the logfile as soon as it's started, not as soon as it sees
* data.
+ *
+ * -v option added Feb, 2008. Verbose output of command line parsing.
*/
#include "apr_general.h"
#include "apr_time.h"
#include "apr_getopt.h"
-#include "apr_signal.h"
#if APR_HAVE_STDLIB_H
#include <stdlib.h>
#endif
-#if APR_HAVE_STRING_H
-#include <string.h>
-#endif
-#if APR_HAVE_STRINGS_H
-#include <strings.h>
-#endif
+#define APR_WANT_STRFUNC
+#include "apr_want.h"
#define BUFSIZE 65536
#define ERRMSGSZ 256
#define MAX_PATH 1024
#endif
-#ifdef SIGHUP
-#ifdef SIGINT
-#define HAVE_SIGNALS 1
-#define SIG_CHECK SIGHUP
-#define SIG_FORCE SIGINT
-#endif
-#endif
-
-#define REASON_LOG 0
-#define REASON_CHECK 1
-#define REASON_FORCE 2
+#define ROTATE_NONE 0
+#define ROTATE_NEW 1
+#define ROTATE_TIME 2
+#define ROTATE_SIZE 3
+#define ROTATE_FORCE 4
+
+static const char *ROTATE_REASONS[] = {
+ "None",
+ "Open a new file",
+ "Time interval expired",
+ "Maximum size reached",
+ "Forced rotation",
+ NULL
+};
typedef struct rotate_config rotate_config_t;
int use_localtime;
int use_strftime;
int force_open;
+ int verbose;
+ int echo;
const char *szLogRoot;
+ int truncate;
+ const char *linkfile;
};
typedef struct rotate_status rotate_status_t;
apr_file_t *nLogFDprev;
char filename[MAX_PATH];
char errbuf[ERRMSGSZ];
- int needsRotate;
- int reason;
+ int rotateReason;
int tLogEnd;
- int now;
int nMessCount;
};
fprintf(stderr, "%s\n", reason);
}
fprintf(stderr,
- "Usage: %s [-l] [-f] <logfile> "
- "{<rotation time in seconds>|<rotation size in megabytes>} "
+ "Usage: %s [-v] [-l] [-L linkname] [-f] [-t] [-e] <logfile> "
+ "{<rotation time in seconds>|<rotation size>(B|K|M|G)} "
"[offset minutes from UTC]\n\n",
argv0);
#ifdef OS2
"starts (N.B. if using a rotation time,\nthe time will always "
"be a multiple of the rotation time, so you can synchronize\n"
"cron scripts with it). At the end of each rotation time or "
- "when the file size\nis reached a new log is started.\n");
+ "when the file size\nis reached a new log is started. If the "
+ "-t option is specified, the specified\nfile will be truncated "
+ "instead of rotated, and is useful where tail is used to\n"
+ "process logs in real time. If the -L option is specified, "
+ "a hard link will be\nmade from the current log file to the "
+ "specified filename. In the case of the -e option, the log "
+ "will be echoed through to stdout for further processing.\n");
exit(1);
}
-static int get_now(int use_localtime, int utc_offset)
+/*
+ * Get the unix time with timezone corrections
+ * given in the config struct.
+ */
+static int get_now(rotate_config_t *config)
{
apr_time_t tNow = apr_time_now();
- if (use_localtime) {
+ int utc_offset = config->utc_offset;
+ if (config->use_localtime) {
/* Check for our UTC offset before using it, since it might
* change if there's a switch between standard and daylight
* savings time.
return (int)apr_time_sec(tNow) + utc_offset;
}
-void closeFile(apr_pool_t *pool, apr_file_t *file) {
+/*
+ * Close a file and destroy the associated pool.
+ */
+static void closeFile(rotate_config_t *config, apr_pool_t *pool, apr_file_t *file)
+{
if (file != NULL) {
+ if (config->verbose) {
+ apr_finfo_t finfo;
+ apr_int32_t wanted = APR_FINFO_NAME;
+ apr_status_t rv = apr_file_info_get(&finfo, wanted, file);
+ if ((rv == APR_SUCCESS) || (rv == APR_INCOMPLETE)) {
+ if (finfo.valid & APR_FINFO_NAME) {
+ fprintf(stderr, "Closing file %s (%s)\n", finfo.name, finfo.fname);
+ } else {
+ fprintf(stderr, "Closing file %s\n", finfo.fname);
+ }
+ }
+ }
apr_file_close(file);
if (pool) {
apr_pool_destroy(pool);
}
}
-void checkRotate(rotate_config_t *config, rotate_status_t *status) {
+/*
+ * Dump the configuration parsing result to STDERR.
+ */
+static void dumpConfig (rotate_config_t *config)
+{
+ fprintf(stderr, "Rotation time interval: %12d\n", config->tRotation);
+ fprintf(stderr, "Rotation size interval: %12d\n", config->sRotation);
+ fprintf(stderr, "Rotation time UTC offset: %12d\n", config->utc_offset);
+ fprintf(stderr, "Rotation based on localtime: %12s\n", config->use_localtime ? "yes" : "no");
+ fprintf(stderr, "Rotation file date pattern: %12s\n", config->use_strftime ? "yes" : "no");
+ fprintf(stderr, "Rotation file forced open: %12s\n", config->force_open ? "yes" : "no");
+ fprintf(stderr, "Rotation verbose: %12s\n", config->verbose ? "yes" : "no");
+ fprintf(stderr, "Rotation file name: %21s\n", config->szLogRoot);
+}
+
+/*
+ * Check whether we need to rotate.
+ * Possible reasons are:
+ * - No log file open (ROTATE_NEW)
+ * - User forces us to rotate (ROTATE_FORCE)
+ * - Our log file size is already bigger than the
+ * allowed maximum (ROTATE_SIZE)
+ * - The next log time interval expired (ROTATE_TIME)
+ *
+ * When size and time constraints are both given,
+ * it suffices that one of them is fulfilled.
+ */
+static void checkRotate(rotate_config_t *config, rotate_status_t *status)
+{
if (status->nLogFD == NULL) {
- status->needsRotate = 1;
- }
- else if (status->reason == REASON_FORCE) {
- status->needsRotate = 1;
- }
- else if (config->tRotation) {
- status->now = get_now(config->use_localtime, config->utc_offset);
- if (status->now >= status->tLogEnd) {
- status->needsRotate = 1;
- }
+ status->rotateReason = ROTATE_NEW;
}
else if (config->sRotation) {
apr_finfo_t finfo;
}
if (current_size > config->sRotation) {
- status->needsRotate = 1;
+ status->rotateReason = ROTATE_SIZE;
+ }
+ else if (config->tRotation) {
+ if (get_now(config) >= status->tLogEnd) {
+ status->rotateReason = ROTATE_TIME;
+ }
+ }
+ }
+ else if (config->tRotation) {
+ if (get_now(config) >= status->tLogEnd) {
+ status->rotateReason = ROTATE_TIME;
}
}
else {
exit(2);
}
- /*
- * Let's close the file before immediately
- * if we got here via a signal.
- */
- if (status->needsRotate &&
- (status->reason != REASON_LOG)) {
- closeFile(status->pfile, status->nLogFD);
- status->nLogFD = NULL;
- status->pfile = NULL;
+ if (status->rotateReason != ROTATE_NONE && config->verbose) {
+ fprintf(stderr, "File rotation needed, reason: %s\n", ROTATE_REASONS[status->rotateReason]);
}
+
return;
}
-void doRotate(rotate_config_t *config, rotate_status_t *status) {
+/*
+ * Open a new log file, and if successful
+ * also close the old one.
+ *
+ * The timestamp for the calculation of the file
+ * name of the new log file will be the actual millisecond
+ * timestamp, except when a regular rotation based on a time
+ * interval is configured and the previous interval
+ * is over. Then the timestamp is the starting time
+ * of the actual interval.
+ */
+static void doRotate(rotate_config_t *config, rotate_status_t *status)
+{
+ int now = get_now(config);
int tLogStart;
apr_status_t rv;
- status->needsRotate = 0;
+ status->rotateReason = ROTATE_NONE;
status->nLogFDprev = status->nLogFD;
status->nLogFD = NULL;
status->pfile_prev = status->pfile;
- status->now = get_now(config->use_localtime, config->utc_offset);
if (config->tRotation) {
int tLogEnd;
- tLogStart = (status->now / config->tRotation) * config->tRotation;
+ tLogStart = (now / config->tRotation) * config->tRotation;
tLogEnd = tLogStart + config->tRotation;
/*
* Check if rotation was forced and the last rotation
* interval is not yet over. Use the value of now instead
* of the time interval boundary for the file name then.
*/
- if ((status->reason == REASON_FORCE) && (tLogStart < status->tLogEnd)) {
- tLogStart = status->now;
+ if (tLogStart < status->tLogEnd) {
+ tLogStart = now;
}
status->tLogEnd = tLogEnd;
}
else {
- tLogStart = status->now;
+ tLogStart = now;
}
if (config->use_strftime) {
apr_strftime(status->filename, &rs, sizeof(status->filename), config->szLogRoot, &e);
}
else {
- sprintf(status->filename, "%s.%010d", config->szLogRoot, tLogStart);
+ if (config->truncate) {
+ apr_snprintf(status->filename, sizeof(status->filename), "%s", config->szLogRoot);
+ }
+ else {
+ apr_snprintf(status->filename, sizeof(status->filename), "%s.%010d", config->szLogRoot,
+ tLogStart);
+ }
}
apr_pool_create(&status->pfile, status->pool);
- rv = apr_file_open(&status->nLogFD, status->filename, APR_WRITE | APR_CREATE | APR_APPEND,
- APR_OS_DEFAULT, status->pfile);
+ if (config->verbose) {
+ fprintf(stderr, "Opening file %s\n", status->filename);
+ }
+ rv = apr_file_open(&status->nLogFD, status->filename, APR_WRITE | APR_CREATE | APR_APPEND
+ | (config->truncate ? APR_TRUNCATE : 0), APR_OS_DEFAULT, status->pfile);
if (rv != APR_SUCCESS) {
char error[120];
}
}
else {
- closeFile(status->pfile_prev, status->nLogFDprev);
+ closeFile(config, status->pfile_prev, status->nLogFDprev);
status->nLogFDprev = NULL;
status->pfile_prev = NULL;
}
status->nMessCount = 0;
- /*
- * Reset marker for signal triggered rotation
- */
- status->reason = REASON_LOG;
+ if (config->linkfile) {
+ apr_file_remove(config->linkfile, status->pfile);
+ if (config->verbose) {
+ fprintf(stderr,"Linking %s to %s\n", status->filename, config->linkfile);
+ }
+ rv = apr_file_link(status->filename, config->linkfile);
+ if (rv != APR_SUCCESS) {
+ char error[120];
+ apr_strerror(rv, error, sizeof error);
+ fprintf(stderr, "Error linking file %s to %s (%s)\n",
+ status->filename, config->linkfile, error);
+ exit(2);
+ }
+ }
}
-#ifdef HAVE_SIGNALS
/*
- * called on SIG_CHECK and SIG_FORCE
+ * Get a size or time param from a string.
+ * Parameter 'last' indicates, whether the
+ * argument is the last commadnline argument.
+ * UTC offset is only allowed as a last argument
+ * in order to make is distinguishable from the
+ * rotation interval time.
*/
-static void external_rotate(int signal)
-{
- /*
- * Set marker for signal triggered rotation
- */
- if (signal == SIG_FORCE) {
- status.reason = REASON_FORCE;
+static const char *get_time_or_size(rotate_config_t *config,
+ const char *arg, int last) {
+ char *ptr = NULL;
+ /* Byte multiplier */
+ unsigned int mult = 1;
+ if ((ptr = strchr(arg, 'B')) != NULL) { /* Found KB size */
+ mult = 1;
}
- else {
- status.reason = REASON_CHECK;
+ else if ((ptr = strchr(arg, 'K')) != NULL) { /* Found KB size */
+ mult = 1024;
}
- /*
- * Close old file conditionally
- */
- checkRotate(&config, &status);
- /*
- * Open new file if force flag was set
- */
- if (config.force_open && status.needsRotate) {
- doRotate(&config, &status);
+ else if ((ptr = strchr(arg, 'M')) != NULL) { /* Found MB size */
+ mult = 1024 * 1024;
+ }
+ else if ((ptr = strchr(arg, 'G')) != NULL) { /* Found GB size */
+ mult = 1024 * 1024 * 1024;
+ }
+ if (ptr) { /* rotation based on file size */
+ if (config->sRotation > 0) {
+ return "Rotation size parameter allowed only once";
+ }
+ if (*(ptr+1) == '\0') {
+ config->sRotation = atoi(arg) * mult;
+ }
+ if (config->sRotation == 0) {
+ return "Invalid rotation size parameter";
+ }
+ }
+ else if ((config->sRotation > 0 || config->tRotation > 0) && last) {
+ /* rotation based on elapsed time */
+ if (config->use_localtime) {
+ return "UTC offset parameter is not valid with -l";
+ }
+ config->utc_offset = atoi(arg) * 60;
}
+ else { /* rotation based on elapsed time */
+ if (config->tRotation > 0) {
+ return "Rotation time parameter allowed only once";
+ }
+ config->tRotation = atoi(arg);
+ if (config->tRotation <= 0) {
+ return "Invalid rotation time parameter";
+ }
+ }
+ return NULL;
}
-#endif
int main (int argc, const char * const argv[])
{
char buf[BUFSIZE];
apr_size_t nRead, nWrite;
apr_file_t *f_stdin;
+ apr_file_t *f_stdout;
apr_getopt_t *opt;
apr_status_t rv;
char c;
- const char *optarg;
- char *ptr = NULL;
+ const char *opt_arg;
+ const char *err = NULL;
apr_app_initialize(&argc, &argv, NULL);
atexit(apr_terminate);
config.use_localtime = 0;
config.use_strftime = 0;
config.force_open = 0;
+ config.verbose = 0;
+ config.echo = 0;
status.pool = NULL;
status.pfile = NULL;
status.pfile_prev = NULL;
status.nLogFD = NULL;
status.nLogFDprev = NULL;
status.tLogEnd = 0;
- status.needsRotate = 0;
- status.reason = REASON_LOG;
- status.now = 0;
+ status.rotateReason = ROTATE_NONE;
status.nMessCount = 0;
apr_pool_create(&status.pool, NULL);
apr_getopt_init(&opt, status.pool, argc, argv);
- while ((rv = apr_getopt(opt, "lf", &c, &optarg)) == APR_SUCCESS) {
+ while ((rv = apr_getopt(opt, "lL:ftve", &c, &opt_arg)) == APR_SUCCESS) {
switch (c) {
case 'l':
config.use_localtime = 1;
break;
+ case 'L':
+ config.linkfile = opt_arg;
+ break;
case 'f':
config.force_open = 1;
break;
+ case 't':
+ config.truncate = 1;
+ break;
+ case 'v':
+ config.verbose = 1;
+ break;
+ case 'e':
+ config.echo = 1;
+ break;
}
}
usage(argv[0], NULL /* specific error message already issued */ );
}
- if (opt->ind + 2 != argc && opt->ind + 3 != argc) {
+ /*
+ * After the initial flags we need 2 to 4 arguments,
+ * the file name, either the rotation interval time or size
+ * or both of them, and optionally the UTC offset.
+ */
+ if ((argc - opt->ind < 2) || (argc - opt->ind > 4) ) {
usage(argv[0], "Incorrect number of arguments");
}
config.szLogRoot = argv[opt->ind++];
- ptr = strchr(argv[opt->ind], 'M');
- if (ptr) { /* rotation based on file size */
- if (*(ptr+1) == '\0') {
- config.sRotation = atoi(argv[opt->ind]) * 1048576;
- }
- if (config.sRotation == 0) {
- usage(argv[0], "Invalid rotation size parameter");
+ /* Read in the remaining flags, namely time, size and UTC offset. */
+ for(; opt->ind < argc; opt->ind++) {
+ if ((err = get_time_or_size(&config, argv[opt->ind],
+ opt->ind < argc - 1 ? 0 : 1)) != NULL) {
+ usage(argv[0], err);
}
}
- else { /* rotation based on elapsed time */
- config.tRotation = atoi(argv[opt->ind]);
- if (config.tRotation <= 0) {
- usage(argv[0], "Invalid rotation time parameter");
- }
- }
- opt->ind++;
-
- if (opt->ind < argc) { /* have UTC offset */
- if (config.use_localtime) {
- usage(argv[0], "UTC offset parameter is not valid with -l");
- }
- config.utc_offset = atoi(argv[opt->ind]) * 60;
- }
config.use_strftime = (strchr(config.szLogRoot, '%') != NULL);
+
if (apr_file_open_stdin(&f_stdin, status.pool) != APR_SUCCESS) {
fprintf(stderr, "Unable to open stdin\n");
exit(1);
}
+ if (apr_file_open_stdout(&f_stdout, status.pool) != APR_SUCCESS) {
+ fprintf(stderr, "Unable to open stdout\n");
+ exit(1);
+ }
+
+ /*
+ * Write out result of config parsing if verbose is set.
+ */
+ if (config.verbose) {
+ dumpConfig(&config);
+ }
+
/*
* Immediately open the logfile as we start, if we were forced
* to do so via '-f'.
doRotate(&config, &status);
}
-#ifdef HAVE_SIGNALS
- apr_signal(SIG_CHECK, external_rotate);
- apr_signal(SIG_FORCE, external_rotate);
-#endif
-
for (;;) {
nRead = sizeof(buf);
-#ifdef HAVE_SIGNALS
- apr_signal_unblock(SIG_CHECK);
- apr_signal_unblock(SIG_FORCE);
-#endif
rv = apr_file_read(f_stdin, buf, &nRead);
-#ifdef HAVE_SIGNALS
- apr_signal_block(SIG_FORCE);
- apr_signal_block(SIG_CHECK);
-#endif
if (rv != APR_SUCCESS) {
exit(3);
}
checkRotate(&config, &status);
- if (status.needsRotate) {
+ if (status.rotateReason != ROTATE_NONE) {
doRotate(&config, &status);
}
else {
status.nMessCount++;
}
+ if (config.echo) {
+ if (apr_file_write_full(f_stdout, buf, nRead, &nWrite)) {
+ fprintf(stderr, "Unable to write to stdout\n");
+ exit(4);
+ }
+ }
}
/* Of course we never, but prevent compiler warnings */
return 0;