Increase LOG_LEVELS enum type safety

Use enum LOG_LEVELS instead of int for log and trace levels.

According to ISO/IEC 9899:2011, section 6.7.2.2
(Enumeration specifiers), paragraph 4: "Each enumerated type
shall be compatible with char, a signed integer type, or an
unsigned integer type. The choice of type is implementation-
defined, but shall be capable of representing the values of
all the members". Section J.3.9 also exposes the implementation
defined nature of enumeration signess.

A negative value (-1) is assigned to objects of type enum
LOG_LEVELS. However, the enumeration type might be implemented
as unsigned integer, because there are only positive enumerators
(enumeration members). Thus, this may cause signess mismatches on
some architectures.
Signed-off-by: 's avatarGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
parent 19bfd099
...@@ -77,7 +77,7 @@ int ptrCompare(void* a, void* b, int value); ...@@ -77,7 +77,7 @@ int ptrCompare(void* a, void* b, int value);
void Heap_check(char* string, void* ptr); void Heap_check(char* string, void* ptr);
void checkEyecatchers(char* file, int line, void* p, size_t size); void checkEyecatchers(char* file, int line, void* p, size_t size);
int Internal_heap_unlink(char* file, int line, void* p); int Internal_heap_unlink(char* file, int line, void* p);
void HeapScan(int log_level); void HeapScan(enum LOG_LEVELS log_level);
/** /**
...@@ -345,7 +345,7 @@ void* Heap_findItem(void* p) ...@@ -345,7 +345,7 @@ void* Heap_findItem(void* p)
* Scans the heap and reports any items currently allocated. * Scans the heap and reports any items currently allocated.
* To be used at shutdown if any heap items have not been freed. * To be used at shutdown if any heap items have not been freed.
*/ */
void HeapScan(int log_level) void HeapScan(enum LOG_LEVELS log_level)
{ {
Node* current = NULL; Node* current = NULL;
...@@ -443,7 +443,7 @@ int HeapDump(FILE* file) ...@@ -443,7 +443,7 @@ int HeapDump(FILE* file)
#if defined(HEAP_UNIT_TESTS) #if defined(HEAP_UNIT_TESTS)
void Log(int log_level, int msgno, char* format, ...) void Log(enum LOG_LEVELS log_level, int msgno, char* format, ...)
{ {
printf("Log %s", format); printf("Log %s", format);
} }
......
...@@ -68,7 +68,7 @@ trace_settings_type trace_settings = ...@@ -68,7 +68,7 @@ trace_settings_type trace_settings =
{ {
TRACE_MINIMUM, TRACE_MINIMUM,
400, 400,
-1 INVALID_LEVEL
}; };
#define MAX_FUNCTION_NAME_LENGTH 256 #define MAX_FUNCTION_NAME_LENGTH 256
...@@ -88,7 +88,7 @@ typedef struct ...@@ -88,7 +88,7 @@ typedef struct
int line; int line;
int has_rc; int has_rc;
int rc; int rc;
int level; enum LOG_LEVELS level;
} traceEntry; } traceEntry;
static int start_index = -1, static int start_index = -1,
...@@ -101,13 +101,13 @@ static char* trace_destination_name = NULL; /**< the name of the trace file */ ...@@ -101,13 +101,13 @@ static char* trace_destination_name = NULL; /**< the name of the trace file */
static char* trace_destination_backup_name = NULL; /**< the name of the backup trace file */ static char* trace_destination_backup_name = NULL; /**< the name of the backup trace file */
static int lines_written = 0; /**< number of lines written to the current output file */ static int lines_written = 0; /**< number of lines written to the current output file */
static int max_lines_per_file = 1000; /**< maximum number of lines to write to one trace file */ static int max_lines_per_file = 1000; /**< maximum number of lines to write to one trace file */
static int trace_output_level = -1; static enum LOG_LEVELS trace_output_level = INVALID_LEVEL;
static Log_traceCallback* trace_callback = NULL; static Log_traceCallback* trace_callback = NULL;
static traceEntry* Log_pretrace(void); static traceEntry* Log_pretrace(void);
static char* Log_formatTraceEntry(traceEntry* cur_entry); static char* Log_formatTraceEntry(traceEntry* cur_entry);
static void Log_output(int log_level, char* msg); static void Log_output(enum LOG_LEVELS log_level, char* msg);
static void Log_posttrace(int log_level, traceEntry* cur_entry); static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry);
static void Log_trace(int log_level, char* buf); static void Log_trace(enum LOG_LEVELS log_level, char* buf);
#if 0 #if 0
FILE* Log_destToFile(char* dest); FILE* Log_destToFile(char* dest);
int Log_compareEntries(char* entry1, char* entry2); int Log_compareEntries(char* entry1, char* entry2);
...@@ -235,7 +235,7 @@ void Log_terminate(void) ...@@ -235,7 +235,7 @@ void Log_terminate(void)
free(trace_destination_backup_name); free(trace_destination_backup_name);
start_index = -1; start_index = -1;
next_index = 0; next_index = 0;
trace_output_level = -1; trace_output_level = INVALID_LEVEL;
sametime_count = 0; sametime_count = 0;
} }
...@@ -328,7 +328,7 @@ static char* Log_formatTraceEntry(traceEntry* cur_entry) ...@@ -328,7 +328,7 @@ static char* Log_formatTraceEntry(traceEntry* cur_entry)
} }
static void Log_output(int log_level, char* msg) static void Log_output(enum LOG_LEVELS log_level, char* msg)
{ {
if (trace_destination) if (trace_destination)
{ {
...@@ -354,7 +354,7 @@ static void Log_output(int log_level, char* msg) ...@@ -354,7 +354,7 @@ static void Log_output(int log_level, char* msg)
} }
static void Log_posttrace(int log_level, traceEntry* cur_entry) static void Log_posttrace(enum LOG_LEVELS log_level, traceEntry* cur_entry)
{ {
if (((trace_output_level == -1) ? log_level >= trace_settings.trace_level : log_level >= trace_output_level)) if (((trace_output_level == -1) ? log_level >= trace_settings.trace_level : log_level >= trace_output_level))
{ {
...@@ -368,7 +368,7 @@ static void Log_posttrace(int log_level, traceEntry* cur_entry) ...@@ -368,7 +368,7 @@ static void Log_posttrace(int log_level, traceEntry* cur_entry)
} }
static void Log_trace(int log_level, char* buf) static void Log_trace(enum LOG_LEVELS log_level, char* buf)
{ {
traceEntry *cur_entry = NULL; traceEntry *cur_entry = NULL;
...@@ -397,7 +397,7 @@ static void Log_trace(int log_level, char* buf) ...@@ -397,7 +397,7 @@ static void Log_trace(int log_level, char* buf)
* @param aFormat the printf format string to be used if the message id does not exist * @param aFormat the printf format string to be used if the message id does not exist
* @param ... the printf inserts * @param ... the printf inserts
*/ */
void Log(int log_level, int msgno, char* format, ...) void Log(enum LOG_LEVELS log_level, int msgno, char* format, ...)
{ {
if (log_level >= trace_settings.trace_level) if (log_level >= trace_settings.trace_level)
{ {
...@@ -436,7 +436,7 @@ void Log(int log_level, int msgno, char* format, ...) ...@@ -436,7 +436,7 @@ void Log(int log_level, int msgno, char* format, ...)
* @param aFormat the printf format string to be used if the message id does not exist * @param aFormat the printf format string to be used if the message id does not exist
* @param ... the printf inserts * @param ... the printf inserts
*/ */
void Log_stackTrace(int log_level, int msgno, int thread_id, int current_depth, const char* name, int line, int* rc) void Log_stackTrace(enum LOG_LEVELS log_level, int msgno, int thread_id, int current_depth, const char* name, int line, int* rc)
{ {
traceEntry *cur_entry = NULL; traceEntry *cur_entry = NULL;
......
...@@ -33,6 +33,7 @@ map LOG_LEVELS ...@@ -33,6 +33,7 @@ map LOG_LEVELS
BE*/ BE*/
enum LOG_LEVELS { enum LOG_LEVELS {
INVALID_LEVEL = -1,
TRACE_MAXIMUM = 1, TRACE_MAXIMUM = 1,
TRACE_MEDIUM, TRACE_MEDIUM,
TRACE_MINIMUM, TRACE_MINIMUM,
...@@ -53,9 +54,9 @@ def trace_settings_type ...@@ -53,9 +54,9 @@ def trace_settings_type
BE*/ BE*/
typedef struct typedef struct
{ {
int trace_level; /**< trace level */ enum LOG_LEVELS trace_level; /**< trace level */
int max_trace_entries; /**< max no of entries in the trace buffer */ int max_trace_entries; /**< max no of entries in the trace buffer */
int trace_output_level; /**< trace level to output to destination */ enum LOG_LEVELS trace_output_level; /**< trace level to output to destination */
} trace_settings_type; } trace_settings_type;
extern trace_settings_type trace_settings; extern trace_settings_type trace_settings;
...@@ -74,8 +75,8 @@ typedef struct ...@@ -74,8 +75,8 @@ typedef struct
int Log_initialize(Log_nameValue*); int Log_initialize(Log_nameValue*);
void Log_terminate(void); void Log_terminate(void);
void Log(int, int, char *, ...); void Log(enum LOG_LEVELS, int, char *, ...);
void Log_stackTrace(int, int, int, int, const char*, int, int*); void Log_stackTrace(enum LOG_LEVELS, int, int, int, const char*, int, int*);
typedef void Log_traceCallback(enum LOG_LEVELS level, char* message); typedef void Log_traceCallback(enum LOG_LEVELS level, char* message);
void Log_setTraceCallback(Log_traceCallback* callback); void Log_setTraceCallback(Log_traceCallback* callback);
......
...@@ -92,7 +92,7 @@ static char* trace_message_list[] = ...@@ -92,7 +92,7 @@ static char* trace_message_list[] =
* @param log_level the log level, used to determine which message list to use * @param log_level the log level, used to determine which message list to use
* @return the message format string * @return the message format string
*/ */
char* Messages_get(int index, int log_level) char* Messages_get(int index, enum LOG_LEVELS log_level)
{ {
char* msg = NULL; char* msg = NULL;
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
#if !defined(MESSAGES_H) #if !defined(MESSAGES_H)
#define MESSAGES_H #define MESSAGES_H
char* Messages_get(int, int); #include "Log.h"
char* Messages_get(int, enum LOG_LEVELS);
#endif #endif
...@@ -101,7 +101,7 @@ int setStack(int create) ...@@ -101,7 +101,7 @@ int setStack(int create)
return cur_thread != NULL; /* good == 1 */ return cur_thread != NULL; /* good == 1 */
} }
void StackTrace_entry(const char* name, int line, int trace_level) void StackTrace_entry(const char* name, int line, enum LOG_LEVELS trace_level)
{ {
Thread_lock_mutex(stack_mutex); Thread_lock_mutex(stack_mutex);
if (!setStack(1)) if (!setStack(1))
...@@ -119,7 +119,7 @@ exit: ...@@ -119,7 +119,7 @@ exit:
} }
void StackTrace_exit(const char* name, int line, void* rc, int trace_level) void StackTrace_exit(const char* name, int line, void* rc, enum LOG_LEVELS trace_level)
{ {
Thread_lock_mutex(stack_mutex); Thread_lock_mutex(stack_mutex);
if (!setStack(0)) if (!setStack(0))
......
...@@ -62,8 +62,8 @@ ...@@ -62,8 +62,8 @@
#endif #endif
#endif #endif
void StackTrace_entry(const char* name, int line, int trace); void StackTrace_entry(const char* name, int line, enum LOG_LEVELS trace);
void StackTrace_exit(const char* name, int line, void* return_value, int trace); void StackTrace_exit(const char* name, int line, void* return_value, enum LOG_LEVELS trace);
void StackTrace_printStack(FILE* dest); void StackTrace_printStack(FILE* dest);
char* StackTrace_get(thread_id_type); char* StackTrace_get(thread_id_type);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment