ckiss

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 758210469db3aa50481d8338206a8190057bebe5
parent 847102b9daba3e2a77f53983223df3a30857e07b
Author: aabacchus <ben@bvnf.space>
Date:   Fri, 21 Apr 2023 19:47:06 +0100

add colour to log functions

Diffstat:
Msrc/kiss.h | 7+++++++
Msrc/libkiss.c | 35+++++++++++++++++++++++++++++++----
Msrc/main.c | 19++++++++++++++++++-
3 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/src/kiss.h b/src/kiss.h @@ -33,10 +33,17 @@ struct env { char date[17]; /* YYYY-MM-DD-HH:MM + '\0' */ }; +/* colours */ +extern char *c1, *c2, *c3; + /* called "mylog" to avoid collision with math.h log function. */ void mylog(const char *s); +void mylog2(const char *name, const char *s); +void mylog_v(char *format, ...); void warn(const char *s); +void warn2(const char *name, const char *s); noreturn void die(const char *s); +noreturn void die2(const char *name, const char *s); noreturn void die_perror(const char *s); /* returns a string containing the concatenation of all args. Args must be diff --git a/src/libkiss.c b/src/libkiss.c @@ -1,3 +1,4 @@ +#include <errno.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -8,23 +9,49 @@ void mylog(const char *s) { - fprintf(stderr, "-> %s\n", s); + fprintf(stderr, "%s-> %s%s\n", c1, c3, s); +} + +void +mylog2(const char *name, const char *s) { + fprintf(stderr, "%s-> %s%s%s %s\n", c1, c2, name, c3, s); } void warn(const char *s) { - fprintf(stderr, "WARNING %s\n", s); + fprintf(stderr, "%sWARNING %s%s\n", c1, c3, s); +} + +void +warn2(const char *name, const char *s) { + fprintf(stderr, "%sWARNING %s%s%s %s\n", c1, c2, name, c3, s); } void die(const char *s) { - mylog(s); + fprintf(stderr, "%sERROR %s%s\n", c1, c3, s); + exit(1); +} + +void +mylog_v(char *format, ...) { + va_list ap; + va_start(ap, format); + fprintf(stderr, "%s-> %s", c1, c3); + vfprintf(stderr, format, ap); + fprintf(stderr, "\n"); + va_end(ap); +} + +void +die2(const char *name, const char *s) { + fprintf(stderr, "%sERROR %s%s%s %s\n", c1, c2, name, c3, s); exit(1); } void die_perror(const char *s) { - perror(s); + die2(s, strerror(errno)); exit(1); } diff --git a/src/main.c b/src/main.c @@ -1,6 +1,8 @@ #include <stdlib.h> #include "kiss.h" +char *c1, *c2, *c3; + noreturn void usage(int r) { mylog("kiss [l] [pkg]..."); @@ -8,11 +10,26 @@ usage(int r) { exit(r); } +void +setup_colors(struct env *e) { + if (!isatty(1) || e->color == 0) { + c1 = ""; + c2 = ""; + c3 = ""; + } else { + c1 = "\033[1;33m"; + c2 = "\033[1;34m"; + c3 = "\033[m"; + } +} + int main(int argc, char **argv) { + struct env *e = setup_env(); + setup_colors(e); + if (argc < 2) usage(0); - struct env *e = setup_env(); switch (argv[1][0]) { case 'l': list(argc - 1, argv + 1, e);