From 803ab3afe58b0bc86e1ac00a0314192b678eadf2 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 29 Nov 2023 05:37:54 +0300 Subject: [PATCH] verbose logging & --quiet command --- main.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 848e2c3..a6000c5 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include #include #define LOG_LEVEL 3 @@ -12,8 +15,17 @@ #define LOG_ERROR 2 #define LOG_DEBUG 3 -// TODO: proper logging with timestamps void log_message(int level, const char *format, ...) { + time_t rawtime; + struct tm *timeinfo; + char buffer[80]; + + time(&rawtime); + timeinfo = localtime(&rawtime); + + strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo); + printf("%s ", buffer); + va_list args; va_start(args, format); if (level <= LOG_LEVEL) { @@ -28,7 +40,21 @@ void close_pseudoterminal(int master, int slave) { log_message(LOG_INFO, "Pseudoterminal closed\n"); } -int main() { +int main(int argc, char *argv[]) { + if (argc > 1 && strcmp(argv[1], "--quiet") == 0) { + int master, slave; + char slave_path[100]; + + if (openpty(&master, &slave, slave_path, NULL, NULL) == -1) { + log_message(LOG_ERROR, "Failed to open pseudoterminal: %s\n", + strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("%s\n", slave_path); + exit(EXIT_SUCCESS); + } + int master, slave; char slave_path[100];