leopard, 一個線上程式碼除錯框架

minizhu發表於2013-09-01

leopard是一個線上程式碼除錯框架。

如果你有下列需求,可以使用這個框架

(1)動態修改線上程式的一些配置引數,比如說,執行緒池的數目,timeout的閾值等

(2)得到線上程式的一些狀態,比如,搜尋引擎中,一個請求對應的排序後文件列表


leopard提供了一個註冊callback機制,使用者只需提供實現自己的callback函式,並註冊到一個唯一的路徑,就可以線上的按照指定的路徑來請求函式,並得到相應的結果。


leopard使用非常簡單,直接嵌入到你的程式中即可。具體使用步驟可以參考提供的例子。


#include <string>
#include <utility>
#include <vector>

#include <boost/bind.hpp>

#include "cpp_leopard/leopard/leopard.h"

using namespace cpp_leopard::leopard;
using namespace std;

// Note:
// For convenience, this file unit does not follow the
// google coding style in a way.

typedef vector<pair<string, string> > vector_pair_type;

string non_member_function(const vector_pair_type& parameters) {
  return "A non-member function";;
}

class Foo {
 public:
  string operator() (const vector_pair_type& parameters) {
    return "A function object";
  }

  string NonStaticMemberFunction(const vector_pair_type& parameters) const {
    return "A non-static member function";
  }

  static string StaticMemberFunction(const vector_pair_type& parameters) {
    return "A static member function";
  }
};

int main() {
  AbstractHttpServer& server = GetDefaultServer();
  RegisterCommonHandlers(&server);

  if (!server.Start()) {
    return -1;
  }

  // Register a free function(non-member function)
  server.RegisterHandler("/function/non_member_function",
                         non_member_function);

  // Register a functor(function object)
  server.RegisterHandler("/function/function_object", Foo());

  // Register a static member function
  server.RegisterHandler("/function/static_member_function",
                         Foo::StaticMemberFunction);

  // Register a member function with boost::bind or std::bindxxx
  // Note: I have a while loop below, so the local var 'foo' will
  // never be destroyed. It's just an example here. Beware don't
  // do this in your project.
  Foo foo;
  server.RegisterHandler("/function/non_static_member_function",
                         boost::bind(&Foo::NonStaticMemberFunction, &foo, _1));

  while (true) {
    sleep(10);
  }
}


注意:

(1)提供了C++的版本,後續會推出java等版本。

(2)不支援跨平臺,目前僅支援unix/linux平臺

(3)leopard採用騰訊開源的blade編譯工具進行編譯

(4)非常輕量級,基本不會影響線上程式的效能

(5)leopard採用一個輕量級的web server,mongoose來提供http通訊服務。 使用者也可以定製,只需實現相應的介面即可。

(6)執行緒安全


具體專案已開源在github上,請關注https://github.com/JianboZhu/leopard

相關文章