raw_ostream.cpp revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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/Support/raw_ostream.h>
10
11#if defined(HAVE_UNISTD_H)
12# include <unistd.h>
13#endif
14
15#if defined(_MSC_VER)
16#include <io.h>
17#ifndef STDIN_FILENO
18# define STDIN_FILENO 0
19#endif
20#ifndef STDOUT_FILENO
21# define STDOUT_FILENO 1
22#endif
23#ifndef STDERR_FILENO
24# define STDERR_FILENO 2
25#endif
26#endif
27
28using namespace mcld;
29
30//===----------------------------------------------------------------------===//
31// raw_ostream
32//===----------------------------------------------------------------------===//
33mcld::raw_fd_ostream::raw_fd_ostream(const char *pFilename,
34                                     std::string &pErrorInfo,
35                                     unsigned int pFlags)
36  : llvm::raw_fd_ostream(pFilename, pErrorInfo, pFlags),
37    m_bConfigColor(false),
38    m_bSetColor(false) {
39}
40
41mcld::raw_fd_ostream::raw_fd_ostream(int pFD,
42                               bool pShouldClose,
43                               bool pUnbuffered)
44  : llvm::raw_fd_ostream(pFD, pShouldClose, pUnbuffered),
45    m_bConfigColor(false),
46    m_bSetColor(false) {
47}
48
49mcld::raw_fd_ostream::~raw_fd_ostream()
50{
51}
52
53void mcld::raw_fd_ostream::setColor(bool pEnable)
54{
55  m_bConfigColor = true;
56  m_bSetColor = pEnable;
57}
58
59llvm::raw_ostream &
60mcld::raw_fd_ostream::changeColor(enum llvm::raw_ostream::Colors pColor,
61                                  bool pBold,
62                                  bool pBackground)
63{
64  if (!is_displayed())
65    return *this;
66  return llvm::raw_fd_ostream::changeColor(pColor, pBold, pBackground);
67}
68
69llvm::raw_ostream& mcld::raw_fd_ostream::resetColor()
70{
71  if (!is_displayed())
72    return *this;
73  return llvm::raw_fd_ostream::resetColor();
74}
75
76llvm::raw_ostream& mcld::raw_fd_ostream::reverseColor()
77{
78  if (!is_displayed())
79    return *this;
80  return llvm::raw_ostream::reverseColor();
81}
82
83bool mcld::raw_fd_ostream::is_displayed() const
84{
85  if (m_bConfigColor)
86    return m_bSetColor;
87
88  return llvm::raw_fd_ostream::is_displayed();
89}
90
91//===----------------------------------------------------------------------===//
92//  outs(), errs(), nulls()
93//===----------------------------------------------------------------------===//
94mcld::raw_fd_ostream& mcld::outs() {
95  // Set buffer settings to model stdout behavior.
96  // Delete the file descriptor when the program exists, forcing error
97  // detection. If you don't want this behavior, don't use outs().
98  static mcld::raw_fd_ostream S(STDOUT_FILENO, true);
99  return S;
100}
101
102mcld::raw_fd_ostream& mcld::errs() {
103  // Set standard error to be unbuffered by default.
104  static mcld::raw_fd_ostream S(STDERR_FILENO, false, true);
105  return S;
106}
107
108