1//===- raw_ostream.h ------------------------------------------------------===//
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#ifndef MCLD_RAW_OSTREAM_H
10#define MCLD_RAW_OSTREAM_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14#include <string>
15#include <llvm/Support/raw_ostream.h>
16#include <mcld/MC/MCLDInfo.h>
17
18namespace mcld
19{
20
21class raw_fd_ostream : public llvm::raw_fd_ostream
22{
23public:
24  /// raw_fd_ostream - Open the specified file for writing. If an error occurs,
25  /// information about the error is put into ErrorInfo, and the stream should
26  /// be immediately destroyed; the string will be empty if no error occurred.
27  /// This allows optional flags to control how the file will be opened.
28  ///
29  /// As a special case, if Filename is "-", then the stream will use
30  /// STDOUT_FILENO instead of opening a file. Note that it will still consider
31  /// itself to own the file descriptor. In particular, it will close the
32  /// file descriptor when it is done (this is necessary to detect
33  /// output errors).
34  raw_fd_ostream(const char *pFilename,
35                 std::string &pErrorInfo,
36                 unsigned int pFlags = 0,
37                 const MCLDInfo* pLDInfo = NULL);
38
39  /// raw_fd_ostream ctor - FD is the file descriptor that this writes to.  If
40  /// ShouldClose is true, this closes the file when the stream is destroyed.
41  raw_fd_ostream(int pFD, bool pShouldClose,
42                 bool pUnbuffered=false,
43                 const MCLDInfo* pLDInfo = NULL);
44
45  virtual ~raw_fd_ostream();
46
47  void setLDInfo(const MCLDInfo& pLDInfo);
48
49
50  llvm::raw_ostream &changeColor(enum llvm::raw_ostream::Colors pColors,
51                                 bool pBold=false,
52                                 bool pBackground=false);
53
54  llvm::raw_ostream &resetColor();
55
56  // FIXME: migrate to newer LLVM
57  // llvm::raw_ostream &reverseColor();
58
59  bool is_displayed() const;
60
61private:
62  const MCLDInfo* m_pLDInfo;
63
64};
65
66/// InitializeOStreams - This initialize mcld::outs() and mcld::errs().
67/// Call it before you use mcld::outs() and mcld::errs().
68void InitializeOStreams(const MCLDInfo& pLDInfo);
69
70/// outs() - This returns a reference to a raw_ostream for standard output.
71/// Use it like: outs() << "foo" << "bar";
72mcld::raw_fd_ostream &outs();
73
74/// errs() - This returns a reference to a raw_ostream for standard error.
75/// Use it like: errs() << "foo" << "bar";
76mcld::raw_fd_ostream &errs();
77
78} // namespace of mcld
79
80#endif
81
82