Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sudo-project
GitHub Repository: sudo-project/sudo
Path: blob/main/plugins/python/example_debugging.py
1532 views
1
import sudo
2
3
import logging
4
5
6
class DebugDemoPlugin(sudo.Plugin):
7
"""
8
An example sudo plugin demonstrating the debugging capabilities.
9
10
You can install it as an extra IO plugin for example by adding the
11
following line to sudo.conf:
12
Plugin python_io python_plugin.so \
13
ModulePath=<path>/example_debugging.py \
14
ClassName=DebugDemoPlugin
15
16
To see the plugin's debug output, use the following line in sudo.conf:
17
Debug python_plugin.so \
18
/var/log/sudo_python_debug plugin@trace,c_calls@trace
19
^ ^-- the options for the logging
20
^----- the output will be placed here
21
22
The options for the logging is in format of multiple "subsystem@level"
23
separated by commas (",").
24
The most interesting subsystems are:
25
plugin Shows each call of sudo.debug API in the log
26
- py_calls Logs whenever a C function calls into the python module.
27
(For example calling this __init__ function.)
28
c_calls Logs whenever python calls into a C sudo API function
29
30
You can also specify "all" as subsystem name to get the debug messages of
31
all subsystems.
32
33
Other subsystems available:
34
internal logs internal functions of the python language wrapper
35
sudo_cb logs when sudo calls into its plugin API
36
load logs python plugin loading / unloading
37
38
Log levels
39
crit sudo.DEBUG.CRIT --> only critical messages
40
err sudo.DEBUG.ERROR
41
warn sudo.DEBUG.WARN
42
notice sudo.DEBUG.NOTICE
43
diag sudo.DEBUG.DIAG
44
info sudo.DEBUG.INFO
45
trace sudo.DEBUG.TRACE
46
debug sudo.DEBUG.DEBUG --> very extreme verbose debugging
47
48
See the sudo.conf manual for more details ("man sudo.conf").
49
50
"""
51
52
def __init__(self, plugin_options, **kwargs):
53
# Specify: "py_calls@info" debug option to show the call to this
54
# constructor and the arguments passed in
55
56
# Specifying "plugin@err" debug option will show this message
57
# (or any more verbose level)
58
sudo.debug(sudo.DEBUG.ERROR, "My demo purpose plugin shows "
59
"this ERROR level debug message")
60
61
# Specifying "plugin@info" debug option will show this message
62
# (or any more verbose level)
63
sudo.debug(sudo.DEBUG.INFO, "My demo purpose plugin shows "
64
"this INFO level debug message")
65
66
# You can also use python log system, because sudo sets its log handler
67
# on the root logger.
68
# Note that the level of python logging is separate than the one set in
69
# sudo.conf. If using the python logger, each will have effect.
70
logger = logging.getLogger()
71
logger.setLevel(logging.INFO)
72
logger.error("Python log system shows this ERROR level debug message")
73
logger.info("Python log system shows this INFO level debug message")
74
75
# If you raise the level to info or below, the call of the debug
76
# will also be logged.
77
# An example output you will see in the debug log file:
78
# Dec 5 15:19:19 sudo[123040] __init__ @ /.../example_debugging.py:54 debugs:
79
# Dec 5 15:19:19 sudo[123040] My demo purpose plugin shows this ERROR level debug message
80
81
# Specify: "c_calls@diag" debug option to show this call and its
82
# arguments. If you specify info debug level instead ("c_calls@info"),
83
# you will also see the python function and line from which you called
84
# the 'options_as_dict' function.
85
self.plugin_options = sudo.options_as_dict(plugin_options)
86
87