mirror of
https://github.com/NotAShelf/dummy-serial.git
synced 2024-11-01 12:01:15 +00:00
verbose logging & --quiet command
This commit is contained in:
parent
2963223261
commit
803ab3afe5
1 changed files with 28 additions and 2 deletions
30
main.c
30
main.c
|
@ -5,6 +5,9 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define LOG_LEVEL 3
|
#define LOG_LEVEL 3
|
||||||
|
@ -12,8 +15,17 @@
|
||||||
#define LOG_ERROR 2
|
#define LOG_ERROR 2
|
||||||
#define LOG_DEBUG 3
|
#define LOG_DEBUG 3
|
||||||
|
|
||||||
// TODO: proper logging with timestamps
|
|
||||||
void log_message(int level, const char *format, ...) {
|
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_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
if (level <= LOG_LEVEL) {
|
if (level <= LOG_LEVEL) {
|
||||||
|
@ -28,7 +40,21 @@ void close_pseudoterminal(int master, int slave) {
|
||||||
log_message(LOG_INFO, "Pseudoterminal closed\n");
|
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;
|
int master, slave;
|
||||||
char slave_path[100];
|
char slave_path[100];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue