#ifndef _H_WEBSERVER_ENV_H_
#define _H_WEBSERVER_ENV_H_
#include "singleton.h"
#include <string>
#include <map>
#include <vector>
namespace WebServer{
class ServerEnv
{
public:
bool init(int argc, char** argv);
// std::string getAbsolutePath(const std::string& path) const;
// std::string getAbsoluteWorkPath(const std::string& path) const;
// std::string getConfigPath();
private:
std::string m_program;
std::string m_exe;
std::string m_cwd;
};
typedef WebServer::Singleton<ServerEnv> EnvMgr;
}
#endif
源文件
#include "env.h"
#include "log.h"
#include <string.h>
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <stdlib.h>
#include "config.h"
namespace WebServer
{
static WebServer::Logger::ptr g_logger = WEBSERVER_LOG_NAME("system");
bool ServerEnv::init(int argc, char** argv)
{
char link[1024] = {0};
char path[1024] = {0};
sprintf(link,"/proc/%d/exe", getpid());
readlink(link, path, sizeof(path));
m_exe = path;
auto pos = m_exe.find_last_of("/");
m_cwd = m_exe.substr(0,pos) + "/";
m_program = argv[0];
return true;
}
/*
std::string Env::getAbsolutePath(const std::string& path)
{
if(path.empty())
return "/";
if(path[0] == '/')
return path;
return m_cwd + path;
}
std::string Env::getAbsoluteWorkPath(const std::string& path)
{
if(path.empty())
return "/";
if(path[0] == '/')
return path;
static WebServer::ConfigVar<std::string>::ptr g_server_work_path =
WebServer::Config::Lookup<std::string>("server.work_path");
return g_server_work_path->getValue() + "/" + path;
}
std::string Env::getConfigPath()
{
return getAbsolutePath(get("c","conf"));
}
*/
}
但我收到此错误:
/home/li/WebServer/src/env.cc:12:8: error: ‘Logger’ in namespace ‘WebServer::WebServer’ does not name a type
static WebServer::Logger::ptr g_logger = WEBSERVER_LOG_NAME("system");
^
/home/li/WebServer/src/env.cc:14:43: error: definition of ‘bool WebServer::ServerEnv::init(int, char**)’ is not in namespace enclosing ‘WebServer::ServerEnv’ [-fpermissive]
bool ServerEnv::init(int argc, char** argv)
^
/home/lizhichao/WebServer/src/env.cc:59:1: error: expected ‘}’ at end of input
}
为什么命名空间“WebServer”变成命名空间“WebServer::WebServer”