1 //===- raw_ostream.cpp ----------------------------------------------------===//
2 //
3 //                     The MCLinker Project
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 #include "mcld/Config/Config.h"
10 #include "mcld/Support/raw_ostream.h"
11 
12 #if defined(HAVE_UNISTD_H)
13 #include <unistd.h>
14 #endif
15 
16 #if defined(__CYGWIN__) || defined(_MSC_VER) || defined(__MINGW32__)
17 #include <io.h>
18 #endif
19 
20 #if defined(_MSC_VER) || defined(__MINGW32__)
21 #ifndef STDIN_FILENO
22 #define STDIN_FILENO 0
23 #endif
24 #ifndef STDOUT_FILENO
25 #define STDOUT_FILENO 1
26 #endif
27 #ifndef STDERR_FILENO
28 #define STDERR_FILENO 2
29 #endif
30 #endif
31 
32 namespace mcld {
33 
34 //===----------------------------------------------------------------------===//
35 // raw_ostream
36 //===----------------------------------------------------------------------===//
raw_fd_ostream(const char * pFilename,std::error_code & pErrorCode,llvm::sys::fs::OpenFlags pFlags)37 raw_fd_ostream::raw_fd_ostream(const char* pFilename,
38                                std::error_code& pErrorCode,
39                                llvm::sys::fs::OpenFlags pFlags)
40     : llvm::raw_fd_ostream(pFilename, pErrorCode, pFlags),
41       m_bConfigColor(false),
42       m_bSetColor(false) {
43 }
44 
raw_fd_ostream(int pFD,bool pShouldClose,bool pUnbuffered)45 raw_fd_ostream::raw_fd_ostream(int pFD,
46                                bool pShouldClose,
47                                bool pUnbuffered)
48     : llvm::raw_fd_ostream(pFD, pShouldClose, pUnbuffered),
49       m_bConfigColor(false),
50       m_bSetColor(false) {
51 }
52 
~raw_fd_ostream()53 raw_fd_ostream::~raw_fd_ostream() {
54 }
55 
setColor(bool pEnable)56 void raw_fd_ostream::setColor(bool pEnable) {
57   m_bConfigColor = true;
58   m_bSetColor = pEnable;
59 }
60 
changeColor(enum llvm::raw_ostream::Colors pColor,bool pBold,bool pBackground)61 llvm::raw_ostream& raw_fd_ostream::changeColor(
62     enum llvm::raw_ostream::Colors pColor,
63     bool pBold,
64     bool pBackground) {
65   if (!is_displayed())
66     return *this;
67   return llvm::raw_fd_ostream::changeColor(pColor, pBold, pBackground);
68 }
69 
resetColor()70 llvm::raw_ostream& raw_fd_ostream::resetColor() {
71   if (!is_displayed())
72     return *this;
73   return llvm::raw_fd_ostream::resetColor();
74 }
75 
reverseColor()76 llvm::raw_ostream& raw_fd_ostream::reverseColor() {
77   if (!is_displayed())
78     return *this;
79   return llvm::raw_ostream::reverseColor();
80 }
81 
is_displayed() const82 bool raw_fd_ostream::is_displayed() const {
83   if (m_bConfigColor)
84     return m_bSetColor;
85 
86   return llvm::raw_fd_ostream::is_displayed();
87 }
88 
89 //===----------------------------------------------------------------------===//
90 //  outs(), errs(), nulls()
91 //===----------------------------------------------------------------------===//
outs()92 raw_fd_ostream& outs() {
93   // Set buffer settings to model stdout behavior.
94   static raw_fd_ostream S(STDOUT_FILENO, false);
95   return S;
96 }
97 
errs()98 raw_fd_ostream& errs() {
99   // Set standard error to be unbuffered by default.
100   static raw_fd_ostream S(STDERR_FILENO, false, true);
101   return S;
102 }
103 
104 }  // namespace mcld
105