Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ flog_print_error(FlogError error) {
void
flog_usage(void) {
printf(
"%s %s\n"
"%s v%d.%d.%d\n"
"\n"
"Usage:\n"
" %s [options] message\n"
Expand All @@ -73,12 +73,19 @@ flog_usage(void) {
" default, info, debug, error, fault\n"
"\n",
PROGRAM_NAME,
PROGRAM_VERSION,
PROGRAM_VERSION_MAJOR,
PROGRAM_VERSION_MINOR,
PROGRAM_VERSION_PATCH,
PROGRAM_NAME
);
}

void
flog_version(void) {
printf("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
printf("%s v%d.%d.%d\n",
PROGRAM_NAME,
PROGRAM_VERSION_MAJOR,
PROGRAM_VERSION_MINOR,
PROGRAM_VERSION_PATCH
);
}
16 changes: 12 additions & 4 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,28 @@
* Constants, helper functions, and error handling types for common operations.
*/

/* Program information */
#define PROGRAM_NAME "flog"
#define PROGRAM_VERSION "v1.7.3"
#define PROGRAM_VERSION_MAJOR 1
#define PROGRAM_VERSION_MINOR 7
#define PROGRAM_VERSION_PATCH 3

/*! \brief An enumerated type representing error conditions. */
typedef enum FlogErrorData {
/* General errors */
FLOG_ERROR_NONE,
FLOG_ERROR_ALLOC,

/* I/O errors */
FLOG_ERROR_APPEND,
FLOG_ERROR_FILE,
FLOG_ERROR_STAT,

/* Configuration errors */
FLOG_ERROR_LVL,
FLOG_ERROR_MSG,
FLOG_ERROR_OPTS,
FLOG_ERROR_SUBSYS,
FLOG_ERROR_FILE,
FLOG_ERROR_STAT,
FLOG_ERROR_SUBSYS
} FlogError;

/*! \brief Print usage information to stdout stream. */
Expand Down
36 changes: 26 additions & 10 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@

bool is_regular_file_or_pipe(int fd, FlogError *error);

const int subsystem_len = 257;
const int category_len = 257;
const int message_len = 8193;
const size_t subsystem_len = 128;
const size_t category_len = 128;
const size_t message_len = 1024;

FlogConfigLevel flog_config_parse_level(const char *str);

Expand Down Expand Up @@ -205,7 +205,7 @@ flog_config_set_subsystem(FlogConfig *config, const char *subsystem) {
assert(subsystem != NULL);

if (strlcpy(config->subsystem, subsystem, subsystem_len) >= subsystem_len) {
fprintf(stderr, "%s: subsystem name truncated to %d bytes\n", PROGRAM_NAME, subsystem_len - 1);
fprintf(stderr, "%s: subsystem name truncated to %zu bytes\n", PROGRAM_NAME, subsystem_len - 1);
}
}

Expand All @@ -222,7 +222,7 @@ flog_config_set_category(FlogConfig *config, const char *category) {
assert(category != NULL);

if (strlcpy(config->category, category, category_len) >= category_len) {
fprintf(stderr, "%s: category name truncated to %d bytes\n", PROGRAM_NAME, category_len - 1);
fprintf(stderr, "%s: category name truncated to %zu bytes\n", PROGRAM_NAME, category_len - 1);
}
}

Expand Down Expand Up @@ -293,7 +293,7 @@ flog_config_set_message(FlogConfig *config, const char *message) {
assert(message != NULL);

if (strlcpy(config->message, message, message_len) >= message_len) {
fprintf(stderr, "%s: message string was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
fprintf(stderr, "%s: message truncated to %zu bytes\n", PROGRAM_NAME, message_len - 1);
}
}

Expand All @@ -320,7 +320,7 @@ flog_config_set_message_from_args(FlogConfig *config, const char **args) {
}

if (message_truncated) {
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
fprintf(stderr, "%s: message truncated to %zu bytes\n", PROGRAM_NAME, message_len - 1);
}
}

Expand All @@ -329,10 +329,26 @@ flog_config_set_message_from_stream(FlogConfig *config, FILE *restrict stream) {
assert(config != NULL);
assert(stream != NULL);

if (fread(config->message, sizeof(char), message_len, stream) >= message_len) {
fprintf(stderr, "%s: message was truncated to %d bytes\n", PROGRAM_NAME, message_len - 1);
config->message[message_len - 1] = '\0';
config->message[0] = '\0';

// Read message from stream
size_t bytes_read = fread(config->message, sizeof(char), message_len - 1, stream);
if (ferror(stream)) {
fprintf(stderr, "%s: error reading message from stream\n", PROGRAM_NAME);
clearerr(stream);
return;
}

// Ensure safe bounds and null termination
bytes_read = (bytes_read >= message_len - 1) ? message_len - 1 : bytes_read;
config->message[bytes_read] = '\0';

// Check for message truncation
char c;
if (fread(&c, sizeof(char), 1, stream) > 0) {
fprintf(stderr, "%s: message truncated to %zu bytes\n", PROGRAM_NAME, message_len - 1);
}
clearerr(stream);
}

FlogConfigMessageType
Expand Down
6 changes: 3 additions & 3 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#include <stdio.h>
#include "common.h"

extern const int subsystem_len;
extern const int category_len;
extern const int message_len;
extern const size_t subsystem_len;
extern const size_t category_len;
extern const size_t message_len;

/*! \brief An enumerated type representing the log level. */
typedef enum FlogConfigLevelData {
Expand Down
13 changes: 10 additions & 3 deletions test/test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ flog_usage_succeeds(void **state) {

char expected_string[1024] = {0};
sprintf(expected_string,
"%s %s\n"
"%s v%d.%d.%d\n"
"\n"
"Usage:\n"
" %s [options] message\n"
Expand All @@ -129,7 +129,9 @@ flog_usage_succeeds(void **state) {
" default, info, debug, error, fault\n"
"\n",
PROGRAM_NAME,
PROGRAM_VERSION,
PROGRAM_VERSION_MAJOR,
PROGRAM_VERSION_MINOR,
PROGRAM_VERSION_PATCH,
PROGRAM_NAME
);

Expand All @@ -143,7 +145,12 @@ flog_version_succeeds(void **state) {
UNUSED(state);

char expected_string[ERROR_STRING_LEN] = {0};
sprintf(expected_string, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION);
sprintf(expected_string, "%s v%d.%d.%d\n",
PROGRAM_NAME,
PROGRAM_VERSION_MAJOR,
PROGRAM_VERSION_MINOR,
PROGRAM_VERSION_PATCH
);

flog_version();

Expand Down
Loading