• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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__)
17 #include <io.h>
18 #endif
19 
20 #if defined(_MSC_VER) || defined(__MINGW32__)
21 #include <io.h>
22 #ifndef STDIN_FILENO
23 # define STDIN_FILENO 0
24 #endif
25 #ifndef STDOUT_FILENO
26 # define STDOUT_FILENO 1
27 #endif
28 #ifndef STDERR_FILENO
29 # define STDERR_FILENO 2
30 #endif
31 #endif
32 
33 using namespace mcld;
34 
35 //===----------------------------------------------------------------------===//
36 // raw_ostream
37 //===----------------------------------------------------------------------===//
raw_fd_ostream(const char * pFilename,std::string & pErrorInfo,llvm::sys::fs::OpenFlags pFlags)38 mcld::raw_fd_ostream::raw_fd_ostream(const char *pFilename,
39                                      std::string &pErrorInfo,
40                                      llvm::sys::fs::OpenFlags pFlags)
41   : llvm::raw_fd_ostream(pFilename, pErrorInfo, pFlags),
42     m_bConfigColor(false),
43     m_bSetColor(false) {
44 }
45 
raw_fd_ostream(int pFD,bool pShouldClose,bool pUnbuffered)46 mcld::raw_fd_ostream::raw_fd_ostream(int pFD,
47                                bool pShouldClose,
48                                bool pUnbuffered)
49   : llvm::raw_fd_ostream(pFD, pShouldClose, pUnbuffered),
50     m_bConfigColor(false),
51     m_bSetColor(false) {
52 }
53 
~raw_fd_ostream()54 mcld::raw_fd_ostream::~raw_fd_ostream()
55 {
56 }
57 
setColor(bool pEnable)58 void mcld::raw_fd_ostream::setColor(bool pEnable)
59 {
60   m_bConfigColor = true;
61   m_bSetColor = pEnable;
62 }
63 
64 llvm::raw_ostream &
changeColor(enum llvm::raw_ostream::Colors pColor,bool pBold,bool pBackground)65 mcld::raw_fd_ostream::changeColor(enum llvm::raw_ostream::Colors pColor,
66                                   bool pBold,
67                                   bool pBackground)
68 {
69   if (!is_displayed())
70     return *this;
71   return llvm::raw_fd_ostream::changeColor(pColor, pBold, pBackground);
72 }
73 
resetColor()74 llvm::raw_ostream& mcld::raw_fd_ostream::resetColor()
75 {
76   if (!is_displayed())
77     return *this;
78   return llvm::raw_fd_ostream::resetColor();
79 }
80 
reverseColor()81 llvm::raw_ostream& mcld::raw_fd_ostream::reverseColor()
82 {
83   if (!is_displayed())
84     return *this;
85   return llvm::raw_ostream::reverseColor();
86 }
87 
is_displayed() const88 bool mcld::raw_fd_ostream::is_displayed() const
89 {
90   if (m_bConfigColor)
91     return m_bSetColor;
92 
93   return llvm::raw_fd_ostream::is_displayed();
94 }
95 
96 //===----------------------------------------------------------------------===//
97 //  outs(), errs(), nulls()
98 //===----------------------------------------------------------------------===//
outs()99 mcld::raw_fd_ostream& mcld::outs() {
100   // Set buffer settings to model stdout behavior.
101   static mcld::raw_fd_ostream S(STDOUT_FILENO, false);
102   return S;
103 }
104 
errs()105 mcld::raw_fd_ostream& mcld::errs() {
106   // Set standard error to be unbuffered by default.
107   static mcld::raw_fd_ostream S(STDERR_FILENO, false, true);
108   return S;
109 }
110 
111