Browse Source

added output control

Nicolas Winkler 7 years ago
parent
commit
688f1e09da
2 changed files with 93 additions and 0 deletions
  1. 55 0
      src/Logging.cpp
  2. 38 0
      src/Logging.h

+ 55 - 0
src/Logging.cpp

@@ -0,0 +1,55 @@
+#include "Logging.h"
+
+using qlow::Out;
+
+
+Out Out::stdout(std::cout);
+
+
+Out::Out(std::ostream& target) :
+    std::ostream(this),
+    target(target),
+    indentVal(0),
+    firstChar(true)
+{
+}
+
+
+int Out::overflow(int c)
+{
+    // needs indenting on first char
+    if (firstChar)
+        for (int i = 0; i < indentVal; i++)
+            target.put(' ');
+
+    target.put(char(c));
+
+    firstChar = c == '\n';
+
+    return 0;
+}
+
+
+/*std::streamsize Out::xsputn(const char* s, std::streamsize n)
+{
+    target.write(s, n);
+}*/
+
+
+int main()
+{
+    qlow::Out& l = Out::stdout;
+    l << "aha" << std::endl;
+    l.indent(10);
+    l << "aha" << std::endl;
+    l.unindent(10);
+    l << "aha" << std::endl;
+}
+
+
+
+
+
+
+
+

+ 38 - 0
src/Logging.h

@@ -0,0 +1,38 @@
+#ifndef QLOW_LOGGING_H
+#define QLOW_LOGGING_H
+
+#include <iostream>
+#include <stack>
+
+namespace qlow
+{
+    class Out;
+}
+
+
+class qlow::Out :
+    public std::ostream,
+    private std::streambuf
+{
+protected:
+    std::ostream& target;
+    bool firstChar;
+    int indentVal;
+
+public:
+    Out(std::ostream& target);
+    ~Out(void) = default;
+
+    inline void indent(int width = 4) { indentVal += width; }
+    inline void unindent(int width = 4) { indentVal -= width; }
+
+    static Out stdout;
+
+protected:
+    virtual int overflow(int c);
+    //virtual std::streamsize xsputn (const char* s, std::streamsize n);
+};
+
+
+#endif // QLOW_LOGGING_H
+