| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #ifndef SERVERAPPLICATION_H |
| 6 | #define SERVERAPPLICATION_H |
| 7 | |
| 8 | #include "common/lsp/server/servercmdparse.h" |
| 9 | #include "common/lsp/server/stdinjsonrpcparser.h" |
| 10 | |
| 11 | #include <QHash> |
| 12 | |
| 13 | #include <functional> |
| 14 | #include <iostream> |
| 15 | |
| 16 | namespace newlsp { |
| 17 | |
| 18 | class ServerApplicationPrivate; |
| 19 | class ServerApplication : public QObject, ServerCmdParse |
| 20 | { |
| 21 | Q_OBJECT |
| 22 | ServerApplicationPrivate *const d; |
| 23 | public: |
| 24 | struct LogFormat { |
| 25 | std::vector<std::string> perfixs; |
| 26 | LogFormat(const std::vector<std::string> &perfixs = {}) : perfixs(perfixs) {} |
| 27 | std::string perfixsString(){ |
| 28 | std::string perfixsString; |
| 29 | for (auto &&one : perfixs) { |
| 30 | perfixsString += "[" + one + "]" ; |
| 31 | } |
| 32 | return perfixsString; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | struct LogValue |
| 37 | { |
| 38 | template<class T> |
| 39 | T operator << (const T &t){ |
| 40 | return t; |
| 41 | } |
| 42 | |
| 43 | std::string operator << (const QString &qStr){ |
| 44 | return qStr.toStdString(); |
| 45 | } |
| 46 | |
| 47 | std::string operator << (const std::list<QString> &listqStr) |
| 48 | { |
| 49 | std::string ret = "" ; |
| 50 | for (auto qStr: listqStr) { |
| 51 | if (ret.empty()) |
| 52 | ret += "(" ; |
| 53 | else |
| 54 | ret += "," + qStr.toStdString(); |
| 55 | } |
| 56 | ret += ")" ; |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | std::string operator << (const QList<QString> &qListStr) |
| 61 | { |
| 62 | return operator<<(std::list<QString>(qListStr.begin(), qListStr.end())); |
| 63 | } |
| 64 | |
| 65 | std::string operator << (const QStringList &qStrList) |
| 66 | { |
| 67 | return operator<<(std::list<QString>(qStrList.begin(), qStrList.end())); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | struct Stderr : LogFormat, LogValue{ |
| 72 | Stderr(const Stderr &) = delete; |
| 73 | Stderr(const std::vector<std::string> &perfixs = {}) : LogFormat(perfixs) |
| 74 | { std::cerr << perfixsString(); } |
| 75 | ~Stderr() {std::cerr << std::endl;} |
| 76 | template<class T> Stderr& operator << (const T &t) { |
| 77 | std::cerr << LogValue::operator<<(t) << " " ; |
| 78 | return *this; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | struct Stdout : LogFormat, LogValue{ |
| 83 | Stdout(const Stderr &) = delete; |
| 84 | Stdout(const std::vector<std::string> &perfixs = {}) : LogFormat(perfixs) |
| 85 | { std::cout << perfixsString(); } |
| 86 | ~Stdout() {std::cout << std::endl;} |
| 87 | template<class T> Stdout& operator << (const T &t) { |
| 88 | std::cout << LogValue::operator<<(t) << " " ; |
| 89 | return *this; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | ServerApplication(const QCoreApplication &app); |
| 94 | virtual ~ServerApplication(); |
| 95 | void start(); |
| 96 | static ServerApplication *ins(); |
| 97 | Stderr err(const std::vector<std::string> &perfixs); |
| 98 | Stdout out(const std::vector<std::string> &perfixs); |
| 99 | |
| 100 | static QString toProtocolString(int id, const QString method, const QJsonObject ¶ms); |
| 101 | static QString toProtocolString(const QString &method, const QJsonObject ¶ms); |
| 102 | static QString toProtocolString(const QJsonObject &object); |
| 103 | static QString localDateTime(); |
| 104 | |
| 105 | public Q_SLOTS: |
| 106 | void jsonrpcJsonOutput(const QJsonObject &obj); |
| 107 | |
| 108 | private Q_SLOTS: |
| 109 | void identifyJsonObject(const QJsonObject &obj); |
| 110 | |
| 111 | Q_SIGNALS: |
| 112 | void jsonrpcMethod(int id, const QString &method, const QJsonObject ¶ms); |
| 113 | void jsonrpcNotification(const QString &method, const QJsonObject ¶ms); |
| 114 | }; |
| 115 | |
| 116 | } //newlsp |
| 117 | |
| 118 | #define localDataTimeStr newlsp::ServerApplication::localDateTime().toStdString() |
| 119 | #define lspServApp newlsp::ServerApplication::ins() |
| 120 | #define lspServErr lspServApp->err({localDataTimeStr, __FILE__, std::to_string(__LINE__), "Server Error"}) |
| 121 | #define lspServOut lspServApp->out({localDataTimeStr, __FILE__, std::to_string(__LINE__), "Server output"}) |
| 122 | |
| 123 | #endif // SERVERAPPLICATION_H |
| 124 | |