Overview   API Reference  

serial_log.hpp

00001 #if !defined(__SERIAL_LOG_HPP__)
00002 #define __SERIAL_LOG_HPP__
00003 
00004 //
00005 // local includes
00006 //
00007 #include "serial_driver.hpp"
00008 
00012 struct serial_log : public serial_driver, private noncopyable
00013 {
00014     static const int DEFAULT_LINE_SIZE = 69;
00015     static const int MAX_LINE_SIZE     = 256;
00016 
00017   protected:
00018     int  line_size, prefix_size;
00019     char buffer[MAX_LINE_SIZE+1], *ptr;
00020     bool continued;
00021 
00022   public:
00023     inline serial_log(const int line_size=0, const char *prefix=nullptr)
00024         : continued(false)
00025     {
00026         set_line_size(line_size);
00027         prefix_size=prefix ? snprintf(buffer, MAX_LINE_SIZE, "%s | ", prefix) : 0;
00028         if (prefix_size >= MAX_LINE_SIZE) prefix_size=0;
00029         reset();
00030     }
00031 
00032     //
00033     // serial_driver: override name, reset & write
00034     //
00035     virtual inline const char *name(void) const
00036     {
00037         return L4VMM_DRIVER" LOG serial";
00038     }
00039 
00040     virtual inline int reset(void)
00041     {
00042         cr();
00043         continued=false;
00044         return 0;
00045     }
00046 
00047     virtual inline int write(const uint8_t c)
00048     {
00049         switch (c) {
00050             case '\b': if (ptr - buffer > prefix_size) ptr--; break;
00051             //
00052             // linux printk prints \r\n at the end of a line, so skip \r.
00053             //     do not reset the buffer. otherwise the line is lost.
00054             //
00055             case '\r': /* filtered out */     break;
00056             case '\n': nl(); continued=false; break;
00057             default:
00058                 *ptr++=c;
00059                 if (ptr - buffer >= line_size) nl(), continued=true;
00060             break;
00061         }
00062 
00063         return 0;
00064     }
00065 
00066     virtual inline void set_line_size(const int line_size=0)
00067     {
00068         if (line_size <= 0) this->line_size=DEFAULT_LINE_SIZE;
00069         else if (line_size > MAX_LINE_SIZE) this->line_size=MAX_LINE_SIZE;
00070         else this->line_size=line_size;
00071     }
00072 
00073   protected:
00074     inline void cr(void)
00075     {
00076         ptr=buffer + prefix_size;
00077     }
00078 
00079     inline void nl(void)
00080     {
00081         *ptr=0;
00082         if (prefix_size >= 2) buffer[prefix_size - 2]=continued ? ':' : '|';
00083         log::puts(buffer);
00084         cr();
00085     }
00086 };
00087 
00088 #endif
00089 
00090 // ***** end of source ***** //
00091 

L4vmm Reference Manual, written by Mario Schwalbe  © 2006-2008