OsMessageInterceptor.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * OsMessageInterceptor.h - * Support for intercepting OS debug output (calls to os_printf, etc.)
8  *
9  * author mikee47 <mike@sillyhouse.net> Feb 2019
10  *
11  * Provided to assist with tracking down the origin of error messages output by SDK.
12  * This is usually done in conjunction with the debugger or by performing a stack dump
13  * to establish the origin of the problem code.
14  *
15  * Example usage:
16 
17  static OsMessageInterceptor osMessageInterceptor;
18 
19  void handleOsMessage(OsMessage& msg)
20  {
21  debug_w("[OS] %s", msg.getBuffer());
22  if(msg.startsWith("E:M ")) {
23  // Handle memory error
24  } else if(msg.contains(" assert ")) {
25  // Handle 'assert' message
26  }
27  }
28 
29  void init()
30  {
31  ...
32 
33  osMessageInterceptor.begin(handleOsMessage);
34 
35  // Example of 'bad' system call, generates "E:M 0" message
36  os_malloc(0);
37 
38  ...
39  }
40 
41  *
42  *
43  ****/
44 
45 #pragma once
46 
47 #include "Data/Buffer/LineBuffer.h"
48 
53 
59 using OsMessageCallback = void (*)(OsMessage& message);
60 
67 {
68 public:
70  {
71  end();
72  }
73 
80  void begin(OsMessageCallback callback);
81 
85  void end();
86 
87 protected:
88  void putch(char c);
89 
90  static void static_putc(char c)
91  {
92  self->putch(c);
93  }
94 
95 private:
96  OsMessage message;
97  OsMessageCallback callback = nullptr;
98  static OsMessageInterceptor* self;
99 };
void(*)(OsMessage &message) OsMessageCallback
Callback to receive OS message line.
Definition: OsMessageInterceptor.h:59
Class to handle interception of OS messages.
Definition: OsMessageInterceptor.h:67
void end()
Stop message interception and revert to output via uart driver.
void putch(char c)
void begin(OsMessageCallback callback)
Enable message interception.
static void static_putc(char c)
Definition: OsMessageInterceptor.h:90
~OsMessageInterceptor()
Definition: OsMessageInterceptor.h:69