浏览代码

Add missing streambufs to redirect notify output into JavaScript console

rdb 10 年之前
父节点
当前提交
99fdba41f7
共有 2 个文件被更改,包括 186 次插入0 次删除
  1. 127 0
      dtool/src/prc/emscriptenLogStream.cxx
  2. 59 0
      dtool/src/prc/emscriptenLogStream.h

+ 127 - 0
dtool/src/prc/emscriptenLogStream.cxx

@@ -0,0 +1,127 @@
+// Filename: emscriptenLogStream.cxx
+// Created by:  rdb (02Apr15)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#include "emscriptenLogStream.h"
+#include "configVariableString.h"
+
+#ifdef __EMSCRIPTEN__
+
+#include <emscripten.h>
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStreamBuf::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+EmscriptenLogStream::EmscriptenLogStreamBuf::
+EmscriptenLogStreamBuf(int flags) :
+  _flags(flags) {
+
+  // The EmscriptenLogStreamBuf doesn't actually need a buffer--it's happy
+  // writing characters one at a time, since they're just getting
+  // stuffed into a string.  (Although the code is written portably
+  // enough to use a buffer correctly, if we had one.)
+  setg(0, 0, 0);
+  setp(0, 0);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStreamBuf::Destructor
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+EmscriptenLogStream::EmscriptenLogStreamBuf::
+~EmscriptenLogStreamBuf() {
+  sync();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStreamBuf::sync
+//       Access: Public, Virtual
+//  Description: Called by the system ostream implementation when the
+//               buffer should be flushed to output (for instance, on
+//               destruction).
+////////////////////////////////////////////////////////////////////
+int EmscriptenLogStream::EmscriptenLogStreamBuf::
+sync() {
+  streamsize n = pptr() - pbase();
+
+  // Write the characters that remain in the buffer.
+  for (char *p = pbase(); p < pptr(); ++p) {
+    write_char(*p);
+  }
+
+  pbump(-n);  // Reset pptr().
+  return 0;  // EOF to indicate write full.
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStreamBuf::overflow
+//       Access: Public, Virtual
+//  Description: Called by the system ostream implementation when its
+//               internal buffer is filled, plus one character.
+////////////////////////////////////////////////////////////////////
+int EmscriptenLogStream::EmscriptenLogStreamBuf::
+overflow(int ch) {
+  streamsize n = pptr() - pbase();
+
+  if (n != 0 && sync() != 0) {
+    return EOF;
+  }
+
+  if (ch != EOF) {
+    // Write one more character.
+    write_char(ch);
+  }
+
+  return 0;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStreamBuf::write_char
+//       Access: Private
+//  Description: Stores a single character.
+////////////////////////////////////////////////////////////////////
+void EmscriptenLogStream::EmscriptenLogStreamBuf::
+write_char(char c) {
+  if (c == '\n') {
+    // Write a line to the log file.
+    emscripten_log(_flags, "%.*s", _data.size(), _data.c_str());
+    _data.clear();
+  } else {
+    _data += c;
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStream::Constructor
+//       Access: Private
+//  Description:
+////////////////////////////////////////////////////////////////////
+EmscriptenLogStream::
+EmscriptenLogStream(int flags) :
+  ostream(new EmscriptenLogStreamBuf(flags)) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: EmscriptenLogStream::Destructor
+//       Access: Public, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+EmscriptenLogStream::
+~EmscriptenLogStream() {
+  delete rdbuf();
+}
+
+#endif  // __EMSCRIPTEN__

+ 59 - 0
dtool/src/prc/emscriptenLogStream.h

@@ -0,0 +1,59 @@
+// Filename: emscriptenLogStream.h
+// Created by:  rdb (02Apr15)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef EMSCRIPTENLOGSTREAM_H
+#define EMSCRIPTENLOGSTREAM_H
+
+#ifdef __EMSCRIPTEN__
+
+#include "dtoolbase.h"
+#include "notifySeverity.h"
+
+#include <string>
+#include <iostream>
+
+////////////////////////////////////////////////////////////////////
+//       Class : EmscriptenLogStream
+// Description : This is a type of ostream that writes each line
+//               to the JavaScript log window.
+///////////////////////////////////////////////////////////////////
+class EmscriptenLogStream : public ostream {
+private:
+  class EmscriptenLogStreamBuf : public streambuf {
+  public:
+    EmscriptenLogStreamBuf(int flags);
+    virtual ~EmscriptenLogStreamBuf();
+
+  protected:
+    virtual int overflow(int c);
+    virtual int sync();
+
+  private:
+    void write_char(char c);
+
+    int _flags;
+    string _data;
+  };
+
+  EmscriptenLogStream(int flags);
+
+public:
+  virtual ~EmscriptenLogStream();
+
+  friend class Notify;
+};
+
+#endif  // __EMSCRIPTEN__
+
+#endif  // EMSCRIPTENLOGSTREAM_H