Skip to content

Commit

Permalink
Merge pull request #24 from tpiekarski/feature/colorizing-macros
Browse files Browse the repository at this point in the history
Merging feature/colorizing-macros, resolves #14
  • Loading branch information
tpiekarski committed May 22, 2020
2 parents 5eb4277 + 0aaf8a3 commit 530fa57
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ install:

script:
- make clean
- make test colors=0
- make test colors=1
- make test debug=0
- make test debug=1

Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
# You should have received a copy of the GNU General Public License
# along with pretty-printk. If not, see <https://www.gnu.org/licenses/>.
#
#

SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
include $(SELF_DIR)/colors.mk
include $(SELF_DIR)/debug.mk

BUILD=/lib/modules/$(shell uname -r)/build
SHELL:=/bin/bash
ccflags-y := -I$(SELF_DIR) -std=gnu99 -Wall -Wno-declaration-after-statement $(DEBUG_FLAGS)
ccflags-y := -I$(SELF_DIR) -std=gnu99 -Wall -Wno-declaration-after-statement $(DEBUG_FLAGS) $(COLORS)

pp_demo_module-objs := pretty_printk_dump.o pretty_printk_demo.o
obj-m := pp_demo_module.o
Expand Down Expand Up @@ -55,7 +55,7 @@ license:

test:
$(eval module=pp_demo_module)
$(MAKE) license demo debug=$(debug)
$(MAKE) license demo colors=$(colors) debug=$(debug)
@sudo dmesg -C
@sudo insmod $(module).ko
@sudo dmesg
Expand Down
55 changes: 36 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,45 @@ other one it should increase readability when running dmesg.
```c
#include "pretty_printk.h"

// [...]
// [...]

pp_warn("Shortcut for severity level and flushing '\n' character");
pp_debug("Extended metadata while printk-ing with debug=1 or PP_DEBUG");
pp_dump("ics", 10, 'a', "some char array");
pp_warn("Shortcut for severity level and flushing '\\n' character");

// [...]
// Extended Metadata (function, filename and line number)
// pp_debug will only print when PP_DEBUG is defined
// ---
pp_debug("Extended metadata while printk-ing with debug=1 or PP_DEBUG");

// x_pos = // getting a current position
// x = // a maximum of something not yet understood
// Dumping multiple variables with valid type specifiers
// i: integer, c: character, s: character array (string)
// ---
pp_dump("ics", 10, 'a', "some char array");

if (x_pos <= x) {
pp_true("x_pos <= x");
// [...]
} else {
pp_false("x_pos <= x");
// Walking through code
// ---
pp_walker();

// [...]
// Color Output
// pp_<color> will only colorize when either PP_COLORS is defined
// ---
pp_info("Colorize dmesg output with colors=1 or PP_COLORS");
pp_info("Example colors: %s, %s, %s... and a few more.", pp_red("red"),
pp_green("green"), pp_blue("blue"));

// Shortcut-ed output of condition for tracing not-yet-understood logic
// ---
int x_pos = 16;
int x = 64;

goto out;
}
if (x_pos <= x) {
pp_true("x_pos <= x");
} else {
pp_false("x_pos <= x");

pp_walker();
goto out;
}

// [...]
// [...]
```
```sh
Expand All @@ -54,8 +68,10 @@ pp_walker();
[ 12.300011] 1. 10
[ 12.300012] 2. a
[ 12.300013] 3. some char array
[ 12.300015] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 72): x_pos <= x is true
[ 12.300017] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 79): It worked up to this line
[ 12.300015] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 70): It worked up to this line
[ 12.300017] pp_demo_module: Colorize dmesg output with colors=1 or PP_COLORS
[ 12.300018] pp_demo_module: Example colors: red, green, blue... and a few more. Yes, colors are in the output, but not in this README :)
[ 12.300019] pp_demo_module (pretty_printk_demo_init @ pretty_printk_demo.c, 85): x_pos <= x is true
```

For testing, debugging and looking at the features the repository provides a demo module **pp_demo_module** to illustrate
Expand Down Expand Up @@ -93,6 +109,7 @@ If not, see [<https://www.gnu.org/licenses/>](https://www.gnu.org/licenses/).

## [Links](#links)

- FLOZz' MISC, [Colors and formatting (ANSI/VT100 Control sequences)](https://misc.flogisoft.com/bash/tip_colors_and_formatting)
- GNU, GCC, [The C Preprocessor](https://gcc.gnu.org/onlinedocs/cpp/index.html#SEC_Contents)
- Stack Overflow, [\_\_FILE\_\_ macro shows full path](https://stackoverflow.com/questions/8487986/file-macro-shows-full-path)
- Stack Overflow, [How to avoid quotes in shortcut-ed printk...](https://stackoverflow.com/questions/61747599/how-to-avoid-quotes-in-shortcut-ed-printk-macros-inside-linux-kernel-mod)
32 changes: 32 additions & 0 deletions colors.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# pretty-printk::Colors Makefile Include
# <https://github.com/tpiekarski/pretty-printk>
# ---
# Copyright 2020 Thomas Piekarski <[email protected]>
#
# This file is part of pretty-printk - a pretty way to print to
# the Linux Kernel Ring Buffer
#
# pretty-printk is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# pretty-printk is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pretty-printk. If not, see <https://www.gnu.org/licenses/>.
#

#
# Pass colors=1 to enable colorized output (make colors=1).
#

colors = 0

ifeq ($(colors), 1)
COLORS = -DPP_COLORS
endif
4 changes: 3 additions & 1 deletion debug.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
# You should have received a copy of the GNU General Public License
# along with pretty-printk. If not, see <https://www.gnu.org/licenses/>.
#

#
# Pass debug=1 to enable debugging output (make debug=1).
#

# Pass debug=1 to enable debugging output (make debug=1)
debug = 0

ifeq ($(debug),1)
Expand Down
53 changes: 53 additions & 0 deletions pretty_printk.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,57 @@ void pp_dump(char *types, ...);
#define pp_false(...) // no debug output
#endif

#undef pp_black
#undef pp_blue_light
#undef pp_blue
#undef pp_cyan_light
#undef pp_cyan
#undef pp_gray_light
#undef pp_gray
#undef pp_green_light
#undef pp_green
#undef pp_magenta_light
#undef pp_magenta
#undef pp_red_light
#undef pp_red
#undef pp_white
#undef pp_yellow_light
#undef pp_yellow

#ifdef PP_COLORS
#define pp_black(arg) "\e[30m" arg "\e[0m"
#define pp_blue_light(arg) "\e[94m" arg "\e[0m"
#define pp_blue(arg) "\e[34m" arg "\e[0m"
#define pp_cyan_light(arg) "\e[96m" arg "\e[0m"
#define pp_cyan(arg) "\e[36m" arg "\e[0m"
#define pp_gray_light(arg) "\e[37m" arg "\e[0m"
#define pp_gray(arg) "\e[90m" arg "\e[0m"
#define pp_green_light(arg) "\e[92m" arg "\e[0m"
#define pp_green(arg) "\e[32m" arg "\e[0m"
#define pp_magenta_light(arg) "\e[95m" arg "\e[0m"
#define pp_magenta(arg) "\e[35m" arg "\e[0m"
#define pp_red_light(arg) "\e[91m" arg "\e[0m"
#define pp_red(arg) "\e[31m" arg "\e[0m"
#define pp_white(arg) "\e[97m" arg "\e[0m"
#define pp_yellow_light(arg) "\e[93m" arg "\e[0m"
#define pp_yellow(arg) "\e[33m" arg "\e[0m"
#else
#define pp_black(arg) arg
#define pp_blue_light(arg) arg
#define pp_blue(arg) arg
#define pp_cyan_light(arg) arg
#define pp_cyan(arg) arg
#define pp_gray_light(arg) arg
#define pp_gray(arg) arg
#define pp_green_light(arg) arg
#define pp_green(arg) arg
#define pp_magenta_light(arg) arg
#define pp_magenta(arg) arg
#define pp_red_light(arg) arg
#define pp_red(arg) arg
#define pp_white(arg) arg
#define pp_yellow_light(arg) arg
#define pp_yellow(arg) arg
#endif

#endif
27 changes: 19 additions & 8 deletions pretty_printk_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ MODULE_VERSION("0.1");
static int __init pretty_printk_demo_init(void)
{
// External macros for shortcutting severity levels
// Common Metadata (module name)
// ---
pp_emerg("Emergency severity");
pp_crit("Critical severity");
pp_err("Error severity");
Expand All @@ -45,26 +45,39 @@ static int __init pretty_printk_demo_init(void)
pp_info("Information severity");

// Appending multiple arguments
// ---
pp_emerg("Emergency, checking arguments, %s and %i", "AAA", 11);
pp_crit("Critical, checking arguments, %s and %i", "BBB", 22);
pp_err("Error, checking arguments, %s and %i", "CCC", 33);
pp_warn("Warning, checking arguments, %s and %i", "DDD", 44);
pp_note("Notice, checking arguments, %s and %i", "EEE", 55);
pp_info("Information, checking arguments, %s and %i", "FFF", 66);

// Extended Metadata (function, filename and line number)
// pp_debug will only print when _PP_DEBUG is defined
pp_debug("Debugging severity");
pp_debug("Debuggin, checking arguments, %s and %i", "char", 42);

pp_warn("Shortcut for severity level and flushing '\\n' character");

// Extended Metadata (function, filename and line number)
// pp_debug will only print when PP_DEBUG is defined
// ---
pp_debug("Extended metadata while printk-ing with debug=1 or PP_DEBUG");

// Dumping multiple variables with valid type specifiers
// i: integer, c: character, s: character array (string)
// ---
pp_dump("ics", 10, 'a', "some char array");

// Walking through code
// ---
pp_walker();

// Color Output
// pp_<color> will only colorize when either PP_COLORS is defined
// ---
pp_info("Colorize dmesg output with colors=1 or PP_COLORS");
pp_info("Example colors: %s, %s, %s... and a few more.", pp_red("red"),
pp_green("green"), pp_blue("blue"));

// Shortcut-ed output of condition for tracing not-yet-understood logic
// ---
int x_pos = 16;
int x = 64;

Expand All @@ -76,8 +89,6 @@ static int __init pretty_printk_demo_init(void)
goto out;
}

pp_walker();

return 0;

out:
Expand Down

0 comments on commit 530fa57

Please sign in to comment.