| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #include "debug_buffer.h" |
|---|
| 17 | |
|---|
| 18 | #include <stdarg.h> |
|---|
| 19 | |
|---|
| 20 | #include "debug.h" |
|---|
| 21 | #include "compiler.h" |
|---|
| 22 | #include "threading.h" |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * @brief standard constructor |
|---|
| 26 | */ |
|---|
| 27 | DebugBuffer::DebugBuffer () |
|---|
| 28 | { |
|---|
| 29 | DebugBuffer::singletonRef = this; |
|---|
| 30 | |
|---|
| 31 | this->lineCount = 0; |
|---|
| 32 | this->bufferArray[0] = '\0'; |
|---|
| 33 | |
|---|
| 34 | this->setMaxBufferSize(100); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | DebugBuffer* DebugBuffer::singletonRef = NULL; |
|---|
| 38 | std::list<std::string> DebugBuffer::buffer; |
|---|
| 39 | char DebugBuffer::bufferArray[DEBUG_BUFFER_SIZE] = ""; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * @brief standard deconstructor |
|---|
| 44 | */ |
|---|
| 45 | DebugBuffer::~DebugBuffer () |
|---|
| 46 | { |
|---|
| 47 | DebugBuffer::singletonRef = NULL; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * @brief deletes all the Buffers |
|---|
| 52 | */ |
|---|
| 53 | void DebugBuffer::flush() |
|---|
| 54 | { |
|---|
| 55 | this->buffer.clear(); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * @brief adds a new Line to the List of Buffers |
|---|
| 60 | * @param line the Line as in the first argument in printf |
|---|
| 61 | */ |
|---|
| 62 | void DebugBuffer::addBufferLineStatic(const char* line, ...) |
|---|
| 63 | { |
|---|
| 64 | static OrxThread::Mutex DebugBuffer__bufferMutex; |
|---|
| 65 | |
|---|
| 66 | OrxThread::MutexLock bufferLock(&DebugBuffer__bufferMutex); |
|---|
| 67 | |
|---|
| 68 | va_list arguments; |
|---|
| 69 | va_start(arguments, line); |
|---|
| 70 | vsnprintf(DebugBuffer::bufferArray, DEBUG_BUFFER_SIZE, line, arguments); |
|---|
| 71 | va_end(arguments); |
|---|
| 72 | |
|---|
| 73 | #if DEBUG_LEVEL < 3 |
|---|
| 74 | if (DebugBuffer::singletonRef == NULL) |
|---|
| 75 | #endif |
|---|
| 76 | printf(DebugBuffer::bufferArray); |
|---|
| 77 | #if DEBUG_LEVEL < 3 |
|---|
| 78 | else |
|---|
| 79 | #else |
|---|
| 80 | if (DebugBuffer::singletonRef != NULL) |
|---|
| 81 | #endif |
|---|
| 82 | DebugBuffer::singletonRef->addBufferLine(DebugBuffer::bufferArray); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * @brief add a Line to the List of Buffers |
|---|
| 87 | * @param line |
|---|
| 88 | * @param arguments |
|---|
| 89 | * |
|---|
| 90 | * This function Adds one line to the buffer. |
|---|
| 91 | * and displays the line as the First Line of the display-buffer |
|---|
| 92 | */ |
|---|
| 93 | void DebugBuffer::addBufferLine(const char* line) |
|---|
| 94 | { |
|---|
| 95 | std::string inputBuffer = this->keepBuffer + line; |
|---|
| 96 | |
|---|
| 97 | unsigned int lineBegin = 0; |
|---|
| 98 | unsigned int lineEnd = 0; |
|---|
| 99 | // adding all the new Lines |
|---|
| 100 | while (lineEnd < inputBuffer.size()) |
|---|
| 101 | { |
|---|
| 102 | lineBegin = lineEnd; |
|---|
| 103 | lineEnd = inputBuffer.find('\n', (lineBegin == 0) ? 0: ++lineBegin); |
|---|
| 104 | if (likely(lineEnd != std::string::npos )) |
|---|
| 105 | { |
|---|
| 106 | this->lineCount++; |
|---|
| 107 | this->buffer.push_front(inputBuffer.substr(lineBegin, lineEnd - lineBegin)); |
|---|
| 108 | } |
|---|
| 109 | else // No end of Line reached. |
|---|
| 110 | { |
|---|
| 111 | this->keepBuffer = inputBuffer.substr(lineBegin, inputBuffer.size() - lineBegin); |
|---|
| 112 | break; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if (inputBuffer[lineBegin] == '\n') |
|---|
| 116 | lineBegin++, lineEnd++; |
|---|
| 117 | |
|---|
| 118 | if (this->buffer.size() > this->maxBufferSize) |
|---|
| 119 | this->buffer.pop_back(); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * @brief displays some nice output from the Debug |
|---|
| 125 | */ |
|---|
| 126 | void DebugBuffer::debug() const |
|---|
| 127 | { |
|---|
| 128 | PRINT(3)("Debugging output to console (not this debug)\n"); |
|---|
| 129 | |
|---|
| 130 | std::list<std::string>::const_iterator bufferLine; |
|---|
| 131 | for (bufferLine = --this->buffer.end(); bufferLine != this->buffer.begin(); --bufferLine) |
|---|
| 132 | printf((*bufferLine).c_str()); |
|---|
| 133 | } |
|---|