Created
March 23, 2023 14:30
-
-
Save dogukanarat/ce26a752d1be5a9d8f2abf8914812000 to your computer and use it in GitHub Desktop.
Console log functions implemented in C language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <time.h> | |
#define COLOR_BLACK "\033[0;30m" | |
#define COLOR_RED "\033[0;31m" | |
#define COLOR_GREEN "\033[0;32m" | |
#define COLOR_YELLOW "\033[0;33m" | |
#define COLOR_BLUE "\033[0;34m" | |
#define COLOR_PURPLE "\033[0;35m" | |
#define COLOR_CYAN "\033[0;36m" | |
#define COLOR_WHITE "\033[0;37m" | |
typedef enum _ConsoleLogColor | |
{ | |
ConsoleLogColorBlack = 0, | |
ConsoleLogColorRed, | |
ConsoleLogColorGreen, | |
ConsoleLogColorYellow, | |
ConsoleLogColorBlue, | |
ConsoleLogColorPurple, | |
ConsoleLogColorCyan, | |
ConsoleLogColorWhite, | |
} ConsoleLogColor; | |
typedef enum _ConsoleLogType | |
{ | |
ConsoleLogTypeNormal = 0, | |
ConsoleLogTypeWarning, | |
ConsoleLogTypeError, | |
} ConsoleLogType; | |
void consoleLogGetTime(char* buffer, size_t bufferLenght) | |
{ | |
time_t rawtime; | |
struct tm* timeinfo; | |
time(&rawtime); | |
timeinfo = localtime(&rawtime); | |
strftime(buffer, bufferLenght, "%Y-%m-%d %H:%M:%S", timeinfo); | |
} | |
void consoleLogRaw(ConsoleLogType logType, const char* message, const char* endl) | |
{ | |
switch (logType) | |
{ | |
case ConsoleLogTypeNormal: | |
printf(COLOR_WHITE); | |
break; | |
case ConsoleLogTypeWarning: | |
printf(COLOR_YELLOW); | |
break; | |
case ConsoleLogTypeError: | |
printf(COLOR_RED); | |
break; | |
default: | |
break; | |
} | |
printf("%s", message); | |
printf("%s", endl); | |
printf(COLOR_WHITE); | |
} | |
void consoleLogLine(ConsoleLogType logType, const char* message) | |
{ | |
char buffer[80]; | |
consoleLogGetTime(buffer, 80); | |
consoleLogRaw(logType, buffer, " -- "); | |
consoleLogRaw(logType, message, "\n"); | |
} | |
void consoleLog(ConsoleLogType logType, const char* message) | |
{ | |
consoleLogRaw(logType, message, " "); | |
} | |
void clError(const char* message) | |
{ | |
consoleLogLine(ConsoleLogTypeError, message); | |
} | |
void clWarning(const char* message) | |
{ | |
consoleLogLine(ConsoleLogTypeWarning, message); | |
} | |
void clLog(const char* message) | |
{ | |
consoleLogLine(ConsoleLogTypeNormal, message); | |
} | |
int main() | |
{ | |
// Test ConsoleLog | |
clLog("This is a log message"); | |
clWarning("This is a warning message"); | |
clError("This is an error message"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment