1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "Logger.h"
17 #include "Source.h"
18 
19 #include <memory>
20 #include <iostream>
21 
22 namespace aapt {
23 
Log(std::ostream & _out,std::ostream & _err)24 Log::Log(std::ostream& _out, std::ostream& _err) : out(_out), err(_err) {
25 }
26 
27 std::shared_ptr<Log> Logger::sLog(std::make_shared<Log>(std::cerr, std::cerr));
28 
setLog(const std::shared_ptr<Log> & log)29 void Logger::setLog(const std::shared_ptr<Log>& log) {
30     sLog = log;
31 }
32 
error()33 std::ostream& Logger::error() {
34     return sLog->err << "error: ";
35 }
36 
error(const Source & source)37 std::ostream& Logger::error(const Source& source) {
38     return sLog->err << source << ": error: ";
39 }
40 
error(const SourceLine & source)41 std::ostream& Logger::error(const SourceLine& source) {
42     return sLog->err << source << ": error: ";
43 }
44 
warn()45 std::ostream& Logger::warn() {
46     return sLog->err << "warning: ";
47 }
48 
warn(const Source & source)49 std::ostream& Logger::warn(const Source& source) {
50     return sLog->err << source << ": warning: ";
51 }
52 
warn(const SourceLine & source)53 std::ostream& Logger::warn(const SourceLine& source) {
54     return sLog->err << source << ": warning: ";
55 }
56 
note()57 std::ostream& Logger::note() {
58     return sLog->out << "note: ";
59 }
60 
note(const Source & source)61 std::ostream& Logger::note(const Source& source) {
62     return sLog->err << source << ": note: ";
63 }
64 
note(const SourceLine & source)65 std::ostream& Logger::note(const SourceLine& source) {
66     return sLog->err << source << ": note: ";
67 }
68 
SourceLogger(const Source & source)69 SourceLogger::SourceLogger(const Source& source)
70 : mSource(source) {
71 }
72 
error()73 std::ostream& SourceLogger::error() {
74     return Logger::error(mSource);
75 }
76 
error(size_t line)77 std::ostream& SourceLogger::error(size_t line) {
78     return Logger::error(SourceLine{ mSource.path, line });
79 }
80 
warn()81 std::ostream& SourceLogger::warn() {
82     return Logger::warn(mSource);
83 }
84 
warn(size_t line)85 std::ostream& SourceLogger::warn(size_t line) {
86     return Logger::warn(SourceLine{ mSource.path, line });
87 }
88 
note()89 std::ostream& SourceLogger::note() {
90     return Logger::note(mSource);
91 }
92 
note(size_t line)93 std::ostream& SourceLogger::note(size_t line) {
94     return Logger::note(SourceLine{ mSource.path, line });
95 }
96 
97 } // namespace aapt
98