Skip to content

Commit

Permalink
Merge pull request #19 from tpiekarski/feature/debug-switch
Browse files Browse the repository at this point in the history
Merging feature/debug-switch, resolves #5
  • Loading branch information
tpiekarski committed May 3, 2020
2 parents 7fcb473 + 821847c commit 62be4d5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 19 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
#
#

SHELL:=/bin/bash
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
include $(SELF_DIR)/debug.mk

BUILD=/lib/modules/$(shell uname -r)/build
ccflags-y := -Wall
SHELL:=/bin/bash
ccflags-y := -std=gnu99 -Wall -Wno-declaration-after-statement $(DEBUG_FLAGS)
obj-m += pretty_printk_demo.o


all: license clean demo

demo:
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ inside the code and on the other one it should increase readability when running
// [...]

pp_warn("Shortcut for severity level and flushing '\n' character");
pp_debug("Extended metadata like function name, file name and line number while printk-ing");
pp_debug("Extended metadata while printk-ing with debug=1 or PP_DEBUG");

// [...]
```
```sh
[ 3009.387138] pretty_printk_demo: "Shortcut for severity level and flushing '\n' character"
[ 3009.387139] pretty_printk_demo (pretty_printk_demo_init @ pretty_printk_demo.c, 58): "Extended metadata like function name, file name and line number while printk-ing"
[ 8204.477771] pretty_printk_demo: "Shortcut for severity level and flushing '\n' character"
[ 8204.477773] pretty_printk_demo (pretty_printk_demo_init @ pretty_printk_demo.c, 62): "Extended metadata while printk-ing with debug=1 or PP_DEBUG"
```

For testing, playing and looking at the features the repository provides a demo module *pretty_printk_demo* to
Expand Down
32 changes: 32 additions & 0 deletions debug.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# pretty-printk::Debug 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 debug=1 to enable debugging output (make debug=1)
debug = 0

ifeq ($(debug),1)
DEBUG_FLAGS = -O -g -DPP_DEBUG
else
DEBUG_FLAGS = -O2
endif
22 changes: 14 additions & 8 deletions pretty_printk.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@
#define _pp(severity, format, args...) \
printk(severity "%s: " #format "\n", THIS_MODULE->name, ##args)

#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

#define _pp_debug(format, args...) \
printk(KERN_DEBUG "%s (%s @ %s, %i): " #format "\n", \
THIS_MODULE->name, __FUNCTION__, __FILENAME__, __LINE__, \
##args)

#define pp_emerg(args...) _pp(KERN_EMERG, args)
#define pp_alert(args...) _pp(KERN_ALERT, args)
#define pp_crit(args...) _pp(KERN_CRIT, args)
Expand All @@ -47,6 +39,20 @@
#define pp_note(args...) _pp(KERN_NOTICE, args)
#define pp_info(args...) _pp(KERN_INFO, args)

#undef _pp_debug
#undef pp_debug

#ifdef PP_DEBUG
#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define _pp_debug(format, args...) \
printk(KERN_DEBUG "%s (%s @ %s, %i): " #format "\n", \
THIS_MODULE->name, __FUNCTION__, __FILENAME__, __LINE__, \
##args)

#define pp_debug(args...) _pp_debug(args)
#else
#define pp_debug(...) // no debug output
#endif

#endif
19 changes: 13 additions & 6 deletions pretty_printk_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,22 @@ static int __init pretty_printk_demo_init(void)
pp_note("Notice severity");
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);

// Internal macro
_pp(KERN_INFO, "Internal Macro...");
_pp_debug("Internal Macro, Extended meta data for debugging");

pp_info("Checking arguments, %s and %i", "char", 42);
pp_debug("Checking arguments, %s and %i", "char", 42);
// Output for README.md
pp_warn("Shortcut for severity level and flushing '\n' character");
pp_debug("Extended metadata while printk-ing with debug=1 or PP_DEBUG");

return 0;
}
Expand Down

0 comments on commit 62be4d5

Please sign in to comment.