Keep track of bytes written to a file

This commit is contained in:
Nils O. Selåsdal
2014-04-22 23:16:57 +02:00
parent 13c601f3b7
commit b75048ceef
+15 -5
View File
@@ -34,6 +34,7 @@ struct UCLogDestination {
struct { struct {
FILE* file; FILE* file;
char *file_name; //NULL for stderr destinaton char *file_name; //NULL for stderr destinaton
size_t size; //keeps track of no of bytes written
} file; } file;
} type; } type;
}; };
@@ -161,6 +162,7 @@ struct UCLogDestination *uc_log_new_stderr(int log_level, int log_location)
dest->dest_type = UC_LDEST_STDERR; dest->dest_type = UC_LDEST_STDERR;
dest->type.file.file = stderr; dest->type.file.file = stderr;
dest->type.file.size = 0;
dest->log_level = log_level;; dest->log_level = log_level;;
dest->log_location = log_location; dest->log_location = log_location;
@@ -233,6 +235,7 @@ struct UCLogDestination *uc_log_new_file(const char *filename, int log_level, in
dest->dest_type = UC_LDEST_FILE; dest->dest_type = UC_LDEST_FILE;
dest->type.file.file = f; dest->type.file.file = f;
dest->type.file.file_name = name; dest->type.file.file_name = name;
dest->type.file.size = 0;
dest->log_level = log_level;; dest->log_level = log_level;;
dest->log_location = log_location; dest->log_location = log_location;
@@ -387,6 +390,7 @@ int uc_log_reopen_files(void)
if (dest->dest_type == UC_LDEST_FILE) { if (dest->dest_type == UC_LDEST_FILE) {
rc = uc_log_reopen_file(dest); rc = uc_log_reopen_file(dest);
dest->type.file.size = 0;
} }
} }
@@ -407,6 +411,7 @@ static inline void uc_vlog_file(const struct UCLogArgs *args, const char *fmt, v
char time_buf[48]; char time_buf[48];
time_t now; time_t now;
struct tm t; struct tm t;
int rc;
if (dest->type.file.file == NULL) if (dest->type.file.file == NULL)
return; return;
@@ -425,20 +430,25 @@ static inline void uc_vlog_file(const struct UCLogArgs *args, const char *fmt, v
time_buf[sizeof time_buf -1] = 0; time_buf[sizeof time_buf -1] = 0;
if (dest->log_location) { if (dest->log_location) {
fprintf(dest->type.file.file, "[%-7s] %s %s [%s] : ", rc = fprintf(dest->type.file.file, "[%-7s] %s %s [%s] : ",
uc_ll_2_str(args->log_level), time_buf, uc_ll_2_str(args->log_level), time_buf,
args->location, args->module->short_name); args->location, args->module->short_name);
} else { } else {
fprintf(dest->type.file.file, "[%-7s] %s [%s]: ", rc = fprintf(dest->type.file.file, "[%-7s] %s [%s]: ",
uc_ll_2_str(args->log_level), time_buf, args->module->short_name); uc_ll_2_str(args->log_level), time_buf, args->module->short_name);
} }
} }
if (rc >= 0) {
dest->type.file.size += rc;
rc = vfprintf(dest->type.file.file, fmt, ap);
}
vfprintf(dest->type.file.file, fmt, ap); if (rc >= 0) {
dest->type.file.size += rc;
fflush(dest->type.file.file); //strictly not needed for stderr rc = fflush(dest->type.file.file); //strictly not needed for stderr
// though stderr could be redirected to a file // though stderr could be redirected to a file
}
} }
static inline void uc_vlog_syslog(const struct UCLogArgs *args, const char *fmt, va_list ap) static inline void uc_vlog_syslog(const struct UCLogArgs *args, const char *fmt, va_list ap)