1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 #ifndef SHARE_VM_LOGGING_LOGFILESTREAMOUTPUT_HPP
  25 #define SHARE_VM_LOGGING_LOGFILESTREAMOUTPUT_HPP
  26 
  27 #include "logging/logDecorators.hpp"
  28 #include "logging/logOutput.hpp"
  29 #include "utilities/globalDefinitions.hpp"
  30 
  31 class LogDecorations;
  32 
  33 // Base class for all FileStream-based log outputs.
  34 class LogFileStreamOutput : public LogOutput {
  35  protected:
  36   FILE*               _stream;
  37   size_t              _decorator_padding[LogDecorators::Count];
  38 
  39   LogFileStreamOutput(FILE *stream) : _stream(stream) {
  40     for (size_t i = 0; i < LogDecorators::Count; i++) {
  41       _decorator_padding[i] = 0;
  42     }
  43   }
  44 
  45   int write_decorations(char* buffer,
  46                         size_t buffer_length,
  47                         const LogDecorations& decorations);
  48 
  49  public:
  50   virtual int write(const LogDecorations& decorations, const char* msg);
  51   virtual int write(LogMessageBuffer::Iterator msg_iterator);
  52 };
  53 
  54 class LogStdoutOutput : public LogFileStreamOutput {
  55   friend class LogOutput;
  56  private:
  57   static LogStdoutOutput _instance;
  58   LogStdoutOutput() : LogFileStreamOutput(stdout) {
  59     set_config_string("all=off");
  60   }
  61   virtual bool initialize(const char* options) {
  62     return false;
  63   }
  64  public:
  65   virtual const char* name() const {
  66     return "stdout";
  67   }
  68 };
  69 
  70 class LogStderrOutput : public LogFileStreamOutput {
  71   friend class LogOutput;
  72  private:
  73   static LogStderrOutput _instance;
  74   LogStderrOutput() : LogFileStreamOutput(stderr) {
  75     set_config_string("all=warning");
  76   }
  77   virtual bool initialize(const char* options) {
  78     return false;
  79   }
  80  public:
  81   virtual const char* name() const {
  82     return "stderr";
  83   }
  84 };
  85 
  86 #endif // SHARE_VM_LOGGING_LOGFILESTREAMOUTPUT_HPP