File indexing completed on 2024-05-05 05:45:56

0001 /*
0002     Copyright Hannah von Reth <vonreth@kde.org>
0003 
0004     Redistribution and use in source and binary forms, with or without
0005     modification, are permitted provided that the following conditions
0006     are met:
0007     1. Redistributions of source code must retain the above copyright
0008        notice, this list of conditions and the following disclaimer.
0009     2. Redistributions in binary form must reproduce the above copyright
0010        notice, this list of conditions and the following disclaimer in the
0011        documentation and/or other materials provided with the distribution.
0012 
0013     THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0014     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0015     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0016     ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0017     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0018     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0019     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0020     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0021     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0022     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0023     SUCH DAMAGE.
0024 */
0025 
0026 #ifndef KSHIM_H
0027 #define KSHIM_H
0028 
0029 #include "config.h"
0030 
0031 #include <iostream>
0032 #include <fstream>
0033 #include <string>
0034 #include <sstream>
0035 #include <vector>
0036 #include <memory>
0037 
0038 #include <filesystem>
0039 
0040 class KShimData;
0041 
0042 namespace KShimLib {
0043 int run(const KShimData &data, const std::vector<KShimLib::string_view> &args);
0044 std::filesystem::path binaryName();
0045 KShimLib::string getenv(const KShimLib::string_view &var,
0046                         const KShimLib::string_view &fallback = {});
0047 bool exists(const std::filesystem::path &path);
0048 
0049 std::filesystem::path findInPath(const std::filesystem::path &path);
0050 
0051 KShimLib::string quote(const KShimLib::string_view &arg);
0052 
0053 KShimLib::string quoteArgs(const std::vector<KShimLib::string_view> &args);
0054 }
0055 
0056 class KLog
0057 {
0058 public:
0059     enum class Type { Debug, Error, Fatal };
0060     KLog(Type t);
0061     KLog(const KLog &other);
0062     ~KLog();
0063 
0064     KLog &log();
0065 
0066     Type type() const;
0067 
0068     static bool loggingEnabled();
0069     static void setLoggingEnabled(bool loggingEnabled);
0070 
0071     static bool getStdLoggingEnabled();
0072     static void setStdLoggingEnabled(bool value);
0073 
0074 private:
0075     bool doLog() const;
0076     Type m_type;
0077     std::shared_ptr<KShimLib::stringstream> m_stream;
0078 
0079     static bool s_loggingEnabled;
0080     static bool s_stdLoggingEnabled;
0081 
0082     friend KLog &operator<<(KLog &log, const std::filesystem::path &t);
0083     friend KLog &operator<<(KLog &log, const std::string &t);
0084 
0085     template<typename T>
0086     friend KLog &operator<<(KLog &, const T &);
0087 };
0088 #define kLog KLog(KLog::Type::Debug).log()
0089 #define kLog2(X) KLog(X).log()
0090 
0091 KLog &operator<<(KLog &log, const std::filesystem::path &t);
0092 KLog &operator<<(KLog &log, const std::string &t);
0093 
0094 template<typename T>
0095 KLog &operator<<(KLog &log, const T &t)
0096 {
0097     if (log.doLog()) {
0098         *log.m_stream << t;
0099     }
0100     return log;
0101 }
0102 
0103 #endif // KSHIM_H