{"id":5641,"date":"2020-05-24T08:13:52","date_gmt":"2020-05-24T08:13:52","guid":{"rendered":"https:\/\/www.fambach.net\/?p=5641"},"modified":"2026-10-03T20:21:40","modified_gmt":"2026-10-03T20:21:40","slug":"debug-ausgaben-im-quellcode","status":"publish","type":"post","link":"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/","title":{"rendered":"Debug output in source code"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Contents\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Boolesche_Variable_IF\" >Boolesche Variable + IF<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Praprozessor_Bedingung\" >Preprocessor directive<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Praprozessor_Ersetzung\" >Preprocessor replacement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Praprozessor_Funktionen\" >Preprocessor directives<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Online_Debugger\" >Online Debugger<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Fazit\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/#Quellen\" >Sources<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Gerade bei neuen Entwicklungen ist es wichtig, ein paar Ausgaben \u00fcber die serielle Schnittstelle oder ein anderes Medium zu schicken. Nat\u00fcrlich k\u00f6nnen diese Ausgaben einfach gel\u00f6scht oder ausdokumentiert werden. Aber sp\u00e4testens, wenn die Implementierung erweitert wird, werden sie wieder ben\u00f6tigt.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Es gibt unterschiedliche Ans\u00e4tze, welche ich hier kurz vorstellen m\u00f6chte. Welche Variante die richtige ist, muss jeder f\u00fcr sich selbst entscheiden.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"boolesche-variable-if\"><span class=\"ez-toc-section\" id=\"Boolesche_Variable_IF\"><\/span>Boolesche Variable + IF<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Verwendung einer globalen booleschen Variable und einer IF-Bedingung f\u00fcr jede Ausgabe.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool debug = true;\n\nvoid funktion1(){\n  \/\/ ....\n  if(debug){\n    Serial.println(\"Debug message\");\n  }\n  \/\/ ....\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Diese Variante erm\u00f6glicht es, das Debugging auch noch w\u00e4hrend des Betriebes einzuschalten. Dies ist bei den folgenden Varianten nicht m\u00f6glich. Der gr\u00f6\u00dfte Nachteil ist der zus\u00e4tzliche Code und der Speicher f\u00fcr die Texte. Um Speicher zu sparen, k\u00f6nnen die Texte ins EEPROM oder in den Flash Speicher ausgelagert werden. Hier ein Beispiel f\u00fcr den Flash Speicher:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool debug = true;\n\nvoid funktion1(){\n  \/\/ ....\n  if(debug){\n    Serial.println(F(\"Debug message\"));\n  }\n  \/\/ ....\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"praeprozessor-bedingung\"><span class=\"ez-toc-section\" id=\"Praprozessor_Bedingung\"><\/span>Preprocessor directive<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Im folgenden Beispiel wird die Variante von oben mit Pr\u00e4prozessoranweisungen realisiert. F\u00e4llt das #define oben weg, werden auch die Ausgaben nicht eincompiliert. Dies spart Speicher ein. Ein Nachteil ist der durchbrochene Code, Pr\u00e4prozessoranweisungen zerfleddern ein wenig das Gesamtbild.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define DEBUG\n\nvoid function1(){\n  \/\/ .... \n#ifdef DEBUG\n   Serial.println(F(&quot;Debug message&quot;));\n#endif\n  \/\/ ....\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"praeprozessor-ersetzung\"><span class=\"ez-toc-section\" id=\"Praprozessor_Ersetzung\"><\/span>Preprocessor replacement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">kaufhaus ein Haus von einer Frau gekauft. <span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/forum.arduino.cc\/index.php?topic=155268.0\" class=\"ub-content-link__anchor\">Arduino Forum<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span> the following example brought:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &quot;Arduino.h&quot;   \n\n#define DEBUG true \/\/ flag to enable\/disable debugging\n#define Serial if(DEBUG)Serial<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dies scheint mir eine der elegantesten Varianten zu sein, weil die Serial Klasse wie gewohnt verwendet werden kann und danach komplett ausgeschaltet wird. Laut einiger Forenteilnehmer scheint dies aber nicht immer zu funktionieren. Somit kommen wir zur letzten Variante.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"praeprozessor-funktionen\"><span class=\"ez-toc-section\" id=\"Praprozessor_Funktionen\"><\/span>Preprocessor directives<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Definition von Pr\u00e4prozessor Funktionen. Diese k\u00f6nnen leicht ausgeschaltet werden und belasten den Speicher nicht. Nachteil: Jede Funktion muss einzeln definiert und im Quellcode verwendet werden. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#ifdef DEBUG\n #define DEBUG_PRINT(x)    Serial.print (x)\n #define DEBUG_PRINTDEC(x) Serial.print (x, DEC)\n #define DEBUG_PRINTLN(x)  Serial.println (x)\n#else\n #define DEBUG_PRINT(x)\n #define DEBUG_PRINTDEC(x)\n #define DEBUG_PRINTLN(x)\n#endif\n\nvoid setup()\n  Serial.begin(115200);\n  DEBUG_PRINT (&quot;Hello, I am a Text2&quot;);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Oder diese im Arduino Forum gefundene Version, hier ist mir nicht ganz klar, wof\u00fcr die Schleifen gebraucht werden \u2026<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define DEBUG false \/\/ flag to enable\/disable debugging\n#define debug_begin(...) do { if (DEBUG) { Serial.begin(__VA_ARGS__); while(!Serial); }} while (0)\n#define debug_print(...) do { if (DEBUG) Serial.print(__VA_ARGS__); } while (0)\n#define debug_println(...) do { if (DEBUG) Serial.println(__VA_ARGS__); } while (0)\n#define debug_printf(...) do { if (DEBUG) Serial.printf(__VA_ARGS__); } while (0)\n\nvoid setup()\n  debug_begin(115200);\n  debug_begin(&quot;Hello, I am a Text2&quot;);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"online-debugger\"><span class=\"ez-toc-section\" id=\"Online_Debugger\"><\/span>Online Debugger<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Even better, of course, is a debugger that can step through the code line by line. This has always been possible with Atmel using JTAG. Arduino seems to have somewhat overslept this possibility. Here are the efforts of the Arduino IDE.<\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-arduino-blog\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"bSI5HPqVfg\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/blog.arduino.cc\/2020\/02\/28\/preview-the-debugger-feature-for-the-arduino-pro-ide\/\" class=\"ub-content-link__anchor\">Preview the Debugger feature for the Arduino Pro IDE<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/blockquote><iframe loading=\"lazy\" title=\"\u201cPreview the Debugger feature for the Arduino Pro IDE\u201d \u2014 Arduino Blog\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"https:\/\/blog.arduino.cc\/2020\/02\/28\/preview-the-debugger-feature-for-the-arduino-pro-ide\/embed\/#?secret=bSI5HPqVfg\" data-secret=\"bSI5HPqVfg\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Here are PlatformIO's aspirations<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/docs.platformio.org\/en\/latest\/plus\/debugging.html\" class=\"ub-content-link__anchor\">https:\/\/docs.platformio.org\/en\/latest\/plus\/debugging.html<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/docs.platformio.org\/en\/latest\/tutorials\/espressif32\/arduino_debugging_unit_testing.html\" class=\"ub-content-link__anchor\">https:\/\/docs.platformio.org\/en\/latest\/tutorials\/espressif32\/arduino_debugging_unit_testing.html<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Eclipse kann es grunds\u00e4tzlich, es gibt auch Ans\u00e4tze f\u00fcr den Arduino:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/www.codeproject.com\/Articles\/1037057\/Debugger-for-Arduino\" class=\"ub-content-link__anchor\">https:\/\/www.codeproject.com\/Articles\/1037057\/Debugger-for-Arduino<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"fazit\"><span class=\"ez-toc-section\" id=\"Fazit\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Es gibt zahlreiche M\u00f6glichkeiten, Debugging Nachrichten aus dem produktiven Code herauszunehmen. Die letzten Varianten sind sicherlich die meist angewandten. Um Variableninhalte \u00fcber alle \u00c4nderungen hinweg zu verfolgen, ist sicherlich ein \u201erichtiger\u201c Debugger besser geeignet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"quellen\"><span class=\"ez-toc-section\" id=\"Quellen\"><\/span>Sources<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/forum.arduino.cc\/index.php?topic=155268.0\" class=\"ub-content-link__anchor\">https:\/\/forum.arduino.cc\/index.php?topic=155268.0<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/blog.arduino.cc\/2020\/02\/28\/preview-the-debugger-feature-for-the-arduino-pro-ide\/\" class=\"ub-content-link__anchor\">Preview the Debugger feature for the Arduino Pro IDE<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/www.arduino.cc\/en\/Reference\/EEPROM\" class=\"ub-content-link__anchor\">https:\/\/www.arduino.cc\/en\/Reference\/EEPROM<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><span class=\"ub-content-link ub-content-link--ok ub-content-link--status-after\"><a href=\"https:\/\/playground.arduino.cc\/Learning\/Memory\/\" class=\"ub-content-link__anchor\">https:\/\/playground.arduino.cc\/Learning\/Memory\/<\/a><span class=\"ub-status ub-status--ok\" title=\"Link status: OK\"><span class=\"ub-status__icon\" aria-hidden=\"true\">\u25cf<\/span><span class=\"screen-reader-text\">Link status: OK<\/span><\/span><\/span><\/p>","protected":false},"excerpt":{"rendered":"<p>Gerade bei neuen Entwicklungen ist es wichtig, ein paar Ausgaben \u00fcber die serielle Schnittstelle oder ein anderes Medium zu schicken. Nat\u00fcrlich k\u00f6nnen diese Ausgaben einfach gel\u00f6scht oder ausdokumentiert werden. Aber sp\u00e4testens, wenn die Implementierung erweitert wird, werden sie wieder ben\u00f6tigt. Es gibt unterschiedliche Ans\u00e4tze, welche ich hier kurz vorstellen m\u00f6chte. Welche Variante die richtige ist, &#8230;<\/p>\n<a href=\"https:\/\/www.fambach.net\/en\/debug-ausgaben-im-quellcode\/\" class=\"read-more-link\">Carry on reading <span class=\"screen-reader-text\">\"Debug Ausgaben im Quellcode\"<\/span> <span class=\"meta-nav\">\u2192<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[157,184,171],"tags":[314,311,312,313],"class_list":["post-5641","post","type-post","status-publish","format-standard","hentry","category-atnrob","category-c-c","category-tools","tag-anweisung","tag-debuggen","tag-debugging","tag-praeprozessor","penguin-post","penguin-post-not-sticky"],"modified_by":"sfambach","_links":{"self":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts\/5641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/comments?post=5641"}],"version-history":[{"count":9,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts\/5641\/revisions"}],"predecessor-version":[{"id":17269,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/posts\/5641\/revisions\/17269"}],"wp:attachment":[{"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/media?parent=5641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/categories?post=5641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fambach.net\/en\/wp-json\/wp\/v2\/tags?post=5641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}