-
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#include <strings.h>
+#include <pthread.h>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include "utils.h"
+#include "log.h"
+
+static pthread_t loggingThread;
-bool ICompare(const std::string& a, const std::string& b)
+bool iCompare(const std::string& a, const std::string& b)
{
- return a.length() == b.length() &&
- std::equal(b.begin(), b.end(), a.begin(),
- [](unsigned char a, unsigned char b)
- { return std::tolower(a) == std::tolower(b); });
+ return a.length() == b.length() &&
+ std::equal(b.begin(), b.end(), a.begin(),
+ [](unsigned char a, unsigned char b)
+ { return std::tolower(a) == std::tolower(b); });
}
-bool ICompare(const std::string& a, int a_offset, const std::string& b, int b_offset, int length)
+bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
{
- return static_cast<int>(a.length()) - length >= a_offset &&
- static_cast<int>(b.length()) - length >= b_offset &&
- std::equal(b.begin() + b_offset, b.begin() + b_offset + length, a.begin() + a_offset,
- [](unsigned char a, unsigned char b)
- { return std::tolower(a) == std::tolower(b); });
+ return static_cast<int>(a.length()) - length >= aOffset &&
+ static_cast<int>(b.length()) - length >= bOffset &&
+ std::equal(b.begin() + bOffset, b.begin() + bOffset + length, a.begin() + aOffset,
+ [](unsigned char a, unsigned char b)
+ { return std::tolower(a) == std::tolower(b); });
}
-bool IsManagedAssembly(const std::string& filename)
+bool isManagedAssembly(const std::string& fileName)
{
- return ICompare(filename, filename.size()-4, ".dll", 0, 4) ||
- ICompare(filename, filename.size()-4, ".exe", 0, 4);
+ return iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
+ iCompare(fileName, fileName.size()-4, ".exe", 0, 4);
}
-bool IsNativeImage(const std::string& filename)
+bool isNativeImage(const std::string& fileName)
{
- return ICompare(filename, filename.size()-7, ".ni", 0, 3);
+ return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
}
-std::string ReadSelfPath()
+std::string readSelfPath()
{
- char buff[PATH_MAX];
- ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
- if (len != -1) {
- buff[len] = '\0';
- return std::string(buff);
- }
-
- return "";
+ char buff[PATH_MAX];
+ ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
+ if (len != -1) {
+ buff[len] = '\0';
+ return std::string(buff);
+ }
+
+ return "";
}
-std::string ConcatPath(const std::string& path1, const std::string& path2)
+std::string concatPath(const std::string& path1, const std::string& path2)
{
- std::string path(path1);
- if (path.back() == PATH_SEPARATOR)
- {
- path.append(path2);
- }
- else
- {
- path += PATH_SEPARATOR;
- path.append(path2);
- }
-
- return path;
+ std::string path(path1);
+ if (path.back() == PATH_SEPARATOR) {
+ path.append(path2);
+ } else {
+ path += PATH_SEPARATOR;
+ path.append(path2);
+ }
+
+ return path;
}
-void AppendPath(std::string& path1, const std::string& path2)
+void appendPath(std::string& path1, const std::string& path2)
{
- if (path1.back() == PATH_SEPARATOR)
- {
- path1.append(path2);
- }
- else
- {
- path1 += PATH_SEPARATOR;
- path1.append(path2);
- }
+ if (path1.back() == PATH_SEPARATOR) {
+ path1.append(path2);
+ } else {
+ path1 += PATH_SEPARATOR;
+ path1.append(path2);
+ }
}
-std::string AbsolutePath(const std::string& path)
+std::string absolutePath(const std::string& path)
{
- std::string absPath;
+ std::string absPath;
+ char realPath[PATH_MAX];
+ if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
+ absPath.assign(realPath);
- char realPath[PATH_MAX];
- if (realpath(path.c_str(), realPath) != nullptr && realPath[0] != '\0')
- {
- absPath.assign(realPath);
- }
-
- return absPath;
+ return absPath;
}
-std::string Basename(const std::string& path)
+std::string baseName(const std::string& path)
{
- auto pos = path.find_last_of(PATH_SEPARATOR);
- if (pos != std::string::npos)
- {
- return path.substr(0, pos);
- }
- else
- {
- return std::string(".");
- }
- return path;
+ auto pos = path.find_last_of(PATH_SEPARATOR);
+ if (pos != std::string::npos)
+ return path.substr(0, pos);
+ else
+ return std::string(".");
+ return path;
}
-bool EndWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& filename)
+bool endWithIgnoreCase(const std::string& str1, const std::string& str2, std::string& fileName)
{
- std::string::size_type len1 = str1.length();
- std::string::size_type len2 = str2.length();
- if (len2 > len1) return false;
-
- int i = 0;
- bool result = std::all_of(str1.cend() - len2, str1.end(),
- [&i, &str2] (char x) {
- return std::tolower(x) == std::tolower(str2[i++]);
- });
- if (result)
- {
- filename = str1.substr(0, len1 - len2);
- }
- return result;
+ std::string::size_type len1 = str1.length();
+ std::string::size_type len2 = str2.length();
+ if (len2 > len1)
+ return false;
+
+ int i = 0;
+ bool result = std::all_of(str1.cend() - len2, str1.end(),
+ [&i, &str2] (char x) {
+ return std::tolower(x) == std::tolower(str2[i++]);
+ });
+ if (result)
+ fileName = str1.substr(0, len1 - len2);
+
+ return result;
}
-bool FileNotExist(const std::string& path)
+bool fileNotExist(const std::string& path)
{
- struct stat sb;
- return stat(path.c_str(), &sb) != 0;
+ struct stat sb;
+ return stat(path.c_str(), &sb) != 0;
}
-static bool ExtCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& filename)
+#ifdef NOT_USE_FUNCTION
+static bool extCheckAndGetFileNameIfExist(const std::string& dir, const std::string& ext, struct dirent* entry, std::string& fileName)
{
- std::string fname(entry->d_name);
- if (fname.length() < ext.length() ||
- fname.compare(fname.length() - ext.length(), ext.length(), ext) != 0)
- {
- return false;
- }
- std::string fullname = ConcatPath(dir, entry->d_name);
- switch (entry->d_type)
- {
- case DT_REG: break;
- case DT_LNK:
- case DT_UNKNOWN:
- if (FileNotExist(fullname))
- {
- return false;
- }
- default:
- return false;
- }
-
- filename = fullname;
-
- return true;
+ std::string fName(entry->d_name);
+ if (fName.length() < ext.length() ||
+ fHame.compare(fName.length() - ext.length(), ext.length(), ext) != 0) {
+ return false;
+ }
+
+ std::string fullName = concatPath(dir, entry->d_name);
+ switch (entry->d_type) {
+ case DT_REG: break;
+ case DT_LNK:
+ case DT_UNKNOWN:
+ if (fileNotExist(fullName))
+ return false;
+ default:
+ return false;
+ }
+
+ fileName = fullName;
+
+ return true;
}
+#endif
-std::string StripNIDLL(const std::string& path)
+std::string stripNiDLL(const std::string& path)
{
- std::string npath(path);
- if (path.size() < 5) return npath;
- if (strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
- {
- npath = path.substr(0, path.size()-4);
- }else if (strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
- {
- npath = path.substr(0, path.size()-4);
- }
- if (strncasecmp(npath.c_str() + npath.size() - 3, ".ni", 3))
- {
- return npath.substr(0, npath.size()-3);
- }
- return npath;
+ std::string niPath(path);
+ if (path.size() < 5) return niPath;
+ if (!strncasecmp(path.c_str() + path.size() - 4, ".dll", 4))
+ niPath = path.substr(0, path.size()-4);
+ else if (!strncasecmp(path.c_str() + path.size() - 4, ".exe", 4))
+ niPath = path.substr(0, path.size()-4);
+
+ if (!strncasecmp(niPath.c_str() + niPath.size() - 3, ".ni", 3))
+ return niPath.substr(0, niPath.size()-3);
+
+ return niPath;
}
-std::string JoinStrings(const std::vector<std::string>& strings, const char* const delimeter)
+std::string joinStrings(const std::vector<std::string>& strings, const char* const delimeter)
{
- switch (strings.size())
- {
- case 0:
- return "";
- case 1:
- return strings[0];
- default:
- std::ostringstream os;
- std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
- os << *strings.rbegin();
- return os.str();
- }
+ switch (strings.size()) {
+ case 0:
+ return "";
+ case 1:
+ return strings[0];
+ default:
+ std::ostringstream os;
+ std::copy(strings.begin(), strings.end()-1, std::ostream_iterator<std::string>(os, delimeter));
+ os << *strings.rbegin();
+ return os.str();
+ }
}
-struct AssemblyFile
-{
- std::string noext;
- std::string ext;
+struct AssemblyFile {
+ std::string noExt;
+ std::string ext;
};
bool operator == (const AssemblyFile& lhs, const AssemblyFile& rhs)
{
- return lhs.noext == rhs.noext && lhs.ext == rhs.ext;
+ return lhs.noExt == rhs.noExt && lhs.ext == rhs.ext;
}
-namespace std
+namespace std {
+ template<>
+ struct hash<AssemblyFile> {
+ std::size_t operator () (const AssemblyFile& f) const {
+ const std::size_t h1 = std::hash<std::string>{}(f.noExt);
+ const std::size_t h2 = std::hash<std::string>{}(f.ext);
+
+ return h1 ^ (h2 << 1);
+ }
+ };
+}
+
+void assembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
{
- template<>
- struct hash<AssemblyFile>
- {
- std::size_t operator () (const AssemblyFile& f) const
- {
- const std::size_t h1 = std::hash<std::string>{}(f.noext);
- const std::size_t h2 = std::hash<std::string>{}(f.ext);
-
- return h1 ^ (h2 << 1);
- }
- };
+ std::map<std::string, std::string> assemblyList;
+ std::map<std::string, std::string> tmpList;
+
+ auto reader = [&assemblyList, &tmpList] (const char* path, const char* name) {
+ std::string pathStr(path);
+ if (isManagedAssembly(pathStr)) {
+ std::string dllName = stripNiDLL(name);
+ std::pair<std::map<std::string, std::string>::iterator, bool> ret;
+ ret = tmpList.insert(std::pair<std::string, std::string>(dllName, pathStr));
+ if (ret.second == false) {
+ if (isNativeImage(pathStr))
+ tmpList[dllName] = pathStr;
+ }
+ }
+ };
+
+ for (auto directory : directories) {
+ scanFilesInDir(directory.c_str(), reader, 1);
+ // merge scaned dll list to tpa list.
+ // if the dll is already exist in the list, that is skipped.
+ assemblyList.insert(tmpList.begin(), tmpList.end());
+ }
+
+ std::map<std::string, std::string>::iterator it;
+ for (it = assemblyList.begin(); it != assemblyList.end(); it++)
+ tpaList += it->second + ':';
+
+ if (tpaList.back() == ':')
+ tpaList.pop_back();
}
-void AssembliesInDirectory(const std::vector<std::string>& directories, std::string& tpaList)
+void scanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
{
- std::unordered_map<AssemblyFile, bool> addedAssemblies;
-
- auto reader = [&addedAssemblies] (const char* path)
- {
- std::string _path(path);
-
- std::string::size_type dotp = _path.rfind('.');
- std::string ext = dotp != std::string::npos ? _path.substr(dotp) : "";
- std::string noext;
- bool ni = false;
-
- if (IsManagedAssembly(_path))
- {
- if (IsNativeImage(_path))
- {
- noext = _path.substr(0, _path.size()-7);
- ni = true;
- }
- else
- {
- noext = _path.substr(0, _path.size()-4);
- }
-
- AssemblyFile f = {noext, ext};
- if (addedAssemblies.find(f) == addedAssemblies.end())
- {
- addedAssemblies[f] = ni;
- }
- else
- {
- addedAssemblies[f] |= ni;
- }
+ DIR *dir;
+ struct dirent* entry;
+ bool isDir;
+
+ dir = opendir(directory);
+
+ if (dir == nullptr)
+ return;
+
+ std::vector<std::string> innerDirectories;
+
+ while ((entry = readdir(dir)) != nullptr) {
+ isDir = false;
+ std::string path = concatPath(directory, entry->d_name);
+ switch (entry->d_type) {
+ case DT_REG: break;
+ case DT_DIR:
+ isDir = true;
+ break;
+ case DT_LNK:
+ case DT_UNKNOWN:
+ struct stat sb;
+ if (stat(path.c_str(), &sb) == -1)
+ continue;
+
+ if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
+ break;
+ default:
+ continue;
+ }
+ if (!isDir)
+ reader(path.c_str(), entry->d_name);
+ else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
+ innerDirectories.push_back(path);
+ }
+
+ if (depth != 0)
+ for (auto& d : innerDirectories)
+ scanFilesInDir(d.c_str(), reader, depth-1);
+
+ closedir(dir);
+}
+
+static void *stdlog(void*)
+{
+ int pfd[2];
+ ssize_t readSize;
+ char buf[1024];
+
+ if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
+ _DBG("fail to make stdout line-buffered");
+ return 0;
}
- };
- for (auto directory : directories)
- {
- ScanFilesInDir(directory.c_str(), reader, 1);
- }
+ if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
+ _DBG("make stderr unbuffered");
+ return 0;
+ }
- for (auto kv : addedAssemblies)
- {
- tpaList += kv.first.noext + (kv.second ? ".ni" : "") + kv.first.ext + ':';
- }
+ /* create the pipe and redirect stdout and stderr */
+ if (pipe(pfd) < 0) {
+ _DBG("fail to create pipe for logging");
+ return 0;
+ }
- if (tpaList.back() == ':')
- tpaList.pop_back();
-}
+ if (dup2(pfd[1], fileno(stdout)) == -1) {
+ _DBG("fail to duplicate fd to stdout");
+ return 0;
+ }
-void ScanFilesInDir(const char* directory, FileReader reader, unsigned int depth)
-{
- DIR *dir;
- struct dirent* entry;
- bool isDir;
-
- dir = opendir(directory);
-
- if (dir == nullptr)
- {
- //_ERR("Can not open directory : %s", directory);
- return;
- }
-
- std::vector<std::string> innerDirectories;
-
- while ((entry = readdir(dir)) != nullptr)
- {
- isDir = false;
- std::string path = ConcatPath(directory, entry->d_name);
- switch (entry->d_type)
- {
- case DT_REG: break;
- case DT_DIR:
- isDir = true;
- break;
- case DT_LNK:
- case DT_UNKNOWN:
- struct stat sb;
- if (stat(path.c_str(), &sb) == -1)
- {
- continue;
- }
+ if (dup2(pfd[1], fileno(stderr)) == -1) {
+ _DBG("fail to duplicate fd to stderr");
+ return 0;
+ }
- if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
- {
- break;
+ close(pfd[1]);
+
+ while ((readSize = read(pfd[0], buf, sizeof buf - 1)) > 0) {
+ if (buf[readSize - 1] == '\n') {
+ --readSize;
}
- default:
- continue;
- }
- if (!isDir)
- {
- reader(path.c_str());
- }
- else if (depth > 1 && strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
- {
- innerDirectories.push_back(path);
+
+ buf[readSize] = 0;
+
+ _LOGX("%s", buf);
}
- }
- if (depth != 0)
- {
- for (auto& d : innerDirectories)
- {
- ScanFilesInDir(d.c_str(), reader, depth-1);
+ close(pfd[0]);
+
+ return 0;
+}
+
+int runLoggingThread() {
+ /* spawn the logging thread */
+ if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
+ _DBG("fail to create pthread");
+ return -1;
}
- }
- closedir(dir);
+ if (pthread_detach(loggingThread) != 0) {
+ _DBG("fail to detach pthread");
+ return -1;
+ }
+
+ return 0;
}
+