[修改]将filewatcher的日志输出改用kylog
This commit is contained in:
parent
d3cd4a3929
commit
16dc31b313
|
@ -6,7 +6,8 @@ find_package(Qt5Core)
|
|||
include_directories(${Qt5Core_INCLUDE_DIRS})
|
||||
add_library(kyfilewatcher SHARED ${SOURCECODE})
|
||||
add_executable(kyfilewatcher-test test/kyfilewatcher-test.cpp)
|
||||
target_link_libraries(kyfilewatcher-test kyfilewatcher ${Qt5Core_LIBRARIES})
|
||||
target_link_libraries(kyfilewatcher kylog kyconf systemd pthread)
|
||||
target_link_libraries(kyfilewatcher-test kyfilewatcher kylog kyconf systemd pthread ${Qt5Core_LIBRARIES})
|
||||
|
||||
install(TARGETS kyfilewatcher
|
||||
DESTINATION lib/kysdk/kysdk-system)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "libkyfilewatcher.hpp"
|
||||
#include <libkylog.h>
|
||||
#include <errno.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
|
@ -25,7 +26,6 @@ void* KYSDK_FILEWATCHER::fileWatcherThreadRunner(void *inst)
|
|||
rd = read(core->watcherFd, events_buf, EVENTBUFLEN);
|
||||
if (rd < (int)sizeof(struct inotify_event))
|
||||
{
|
||||
// log: no data accessed
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -35,7 +35,6 @@ void* KYSDK_FILEWATCHER::fileWatcherThreadRunner(void *inst)
|
|||
break;
|
||||
event = (struct inotify_event *)events_pos;
|
||||
|
||||
// qDebug() << "wfd "<<event->wd<<" get event mask "<<event->mask;
|
||||
char pathName[NAME_MAX];
|
||||
snprintf(pathName, event->len, "%s", event->name);
|
||||
|
||||
|
@ -49,23 +48,33 @@ void* KYSDK_FILEWATCHER::fileWatcherThreadRunner(void *inst)
|
|||
}
|
||||
|
||||
FileWatcher::FileWatcher()
|
||||
: logfile("/tmp/watcher.log")
|
||||
{
|
||||
this->isActived = true;
|
||||
this->quit = false;
|
||||
this->watcherFd = inotify_init1(IN_CLOEXEC);
|
||||
this->logfile.open(QFile::WriteOnly);
|
||||
|
||||
pthread_create(&this->threadRunner, nullptr, fileWatcherThreadRunner, this);
|
||||
if (this->watcherFd <= 0)
|
||||
{
|
||||
klog_err("文件监听启动失败,错误码:%d\n", errno);
|
||||
throw 2;
|
||||
}
|
||||
this->threadRunner = (pthread_t *)malloc(sizeof(pthread_t));
|
||||
if (! this->threadRunner)
|
||||
{
|
||||
klog_err("文件监听启动失败,错误码:%d\n", errno);
|
||||
throw 1;
|
||||
}
|
||||
pthread_create(this->threadRunner, nullptr, fileWatcherThreadRunner, this);
|
||||
}
|
||||
|
||||
FileWatcher::~FileWatcher()
|
||||
{
|
||||
this->quit = true;
|
||||
pthread_cancel(this->threadRunner);
|
||||
pthread_join(this->threadRunner, nullptr);
|
||||
|
||||
this->logfile.close();
|
||||
if (this->threadRunner)
|
||||
{
|
||||
pthread_cancel(*this->threadRunner);
|
||||
pthread_join(*this->threadRunner, nullptr);
|
||||
free(this->threadRunner);
|
||||
}
|
||||
|
||||
this->isActived = false;
|
||||
if (this->watcherFd > 0)
|
||||
|
@ -109,8 +118,6 @@ QStringList FileWatcher::addWatchTargetRecursive(QString url, FileWatcherType ty
|
|||
failedList.append(tmpURL);
|
||||
}
|
||||
|
||||
logfile.close();
|
||||
|
||||
return failedList;
|
||||
}
|
||||
|
||||
|
@ -229,17 +236,18 @@ void FileWatcher::clearWatchList()
|
|||
this->watchList.clear();
|
||||
this->fdCacheMap.clear();
|
||||
|
||||
// log
|
||||
qDebug() << "Watcher List has been cleared.";
|
||||
klog_info("文件监听列表已清空\n");
|
||||
}
|
||||
|
||||
void FileWatcher::pauseWatcher()
|
||||
{
|
||||
klog_info("文件监听已暂停\n");
|
||||
this->isActived = false;
|
||||
}
|
||||
|
||||
void FileWatcher::restartWatcher()
|
||||
{
|
||||
klog_info("文件监听已恢复\n");
|
||||
this->isActived = true;
|
||||
}
|
||||
|
||||
|
@ -370,6 +378,7 @@ int FileWatcher::addWatchFile(FileWatcher::FileDescription node)
|
|||
{
|
||||
// log
|
||||
qDebug() << node.url <<"监听符添加失败:"<<errno;
|
||||
klog_err("文件[%s]监听添加失败,错误码%d\n", node.url.toStdString().c_str(), errno);
|
||||
return -1;
|
||||
}
|
||||
this->fdCacheMap.insert(node.wfd, node.url);
|
||||
|
@ -383,7 +392,7 @@ int FileWatcher::addWatchFile(FileWatcher::FileDescription node)
|
|||
this->updateWatchFileAttribute(node.url, node.attr);
|
||||
}
|
||||
|
||||
// log
|
||||
klog_debug("文件[%s]已加入监听\n", node.url.toStdString().c_str());
|
||||
qDebug() << "Watcher for "<<node.url<<" has been added.";
|
||||
|
||||
return 0;
|
||||
|
@ -400,12 +409,12 @@ int FileWatcher::removeWatchFile(QString url)
|
|||
{
|
||||
fdCacheMap.remove(watchList[url].wfd);
|
||||
watchList.remove(url);
|
||||
// log
|
||||
klog_debug("文件[%s]监听已移除\n", url.toStdString().c_str());
|
||||
qDebug() << "Watcher for "<<url<<" has been removed.";
|
||||
}
|
||||
else
|
||||
{
|
||||
// log
|
||||
klog_err("文件[%s]监听移除失败,错误码:%d\n", url.toStdString().c_str(), errno);
|
||||
qDebug() << "remove watcher for "<<url<<" with wfd" <<this->watchList[url].wfd<<" failed. "<<errno;
|
||||
}
|
||||
}
|
||||
|
@ -481,40 +490,32 @@ QStringList FileWatcher::getChildFile(QString parent, int depth, int maxdepth, i
|
|||
return res;
|
||||
|
||||
int index = 0;
|
||||
char buf[1024];
|
||||
do {
|
||||
QFileInfo finfo = finfolist.at(index);
|
||||
|
||||
if ((recurType & TMPFILE) == 0 && finfo.fileName().startsWith("~"))
|
||||
{
|
||||
sprintf(buf, "%s is tmpfile, skip.\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
klog_info("文件[%s]是临时文件,略过\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
index ++;
|
||||
continue;
|
||||
}
|
||||
if ((recurType & HIDDEN) == 0 && finfo.fileName().startsWith("."))
|
||||
{
|
||||
sprintf(buf, "%s is hidden, skip.\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
klog_info("文件[%s]是隐藏文件,略过\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
index ++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (finfo.isDir())
|
||||
{
|
||||
sprintf(buf, "%s is dir, search for it's subfile.\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
klog_info("文件[%s]是一个目录,略过\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
res.append(this->getChildFile(finfo.absoluteFilePath(), depth + 1, maxdepth));
|
||||
if (recurType & DIR)
|
||||
res.append(finfo.absoluteFilePath());
|
||||
sprintf(buf, "append %s\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
}
|
||||
else
|
||||
{
|
||||
res.append(finfo.absoluteFilePath());
|
||||
sprintf(buf, "append %s\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
}
|
||||
index ++;
|
||||
}while (index < finfolist.size());
|
||||
|
@ -546,7 +547,6 @@ QStringList FileWatcher::getChildDir(QString parent, int depth, int maxdepth, in
|
|||
return res;
|
||||
|
||||
int index = 0;
|
||||
char buf[1024];
|
||||
do {
|
||||
QFileInfo finfo = finfolist.at(index);
|
||||
|
||||
|
@ -554,24 +554,18 @@ QStringList FileWatcher::getChildDir(QString parent, int depth, int maxdepth, in
|
|||
{
|
||||
if ((recurType & TMPFILE) == 0 && finfo.fileName().startsWith("~"))
|
||||
{
|
||||
sprintf(buf, "%s is tmpdir, skip.\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
klog_info("文件[%s]是临时文件,略过\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
index ++;
|
||||
continue;
|
||||
}
|
||||
if ((recurType & HIDDEN) == 0 && finfo.fileName().startsWith("."))
|
||||
{
|
||||
sprintf(buf, "%s is hidden, skip.\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
klog_info("文件[%s]是隐藏文件,略过\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
index ++;
|
||||
continue;
|
||||
}
|
||||
sprintf(buf, "%s is dir, search for it's subfile.\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
res.append(this->getChildDir(finfo.absoluteFilePath(), depth + 1, maxdepth));
|
||||
res.append(finfo.absoluteFilePath());
|
||||
sprintf(buf, "append %s\n", finfo.absoluteFilePath().toStdString().c_str());
|
||||
logfile.write(buf);
|
||||
}
|
||||
else
|
||||
break;
|
||||
|
|
|
@ -136,9 +136,7 @@ private:
|
|||
int watcherFd;
|
||||
bool isActived;
|
||||
bool quit;
|
||||
pthread_t threadRunner;
|
||||
|
||||
QFile logfile;
|
||||
pthread_t *threadRunner;
|
||||
|
||||
friend void* fileWatcherThreadRunner(void *inst);
|
||||
int addWatchFile(FileDescription node);
|
||||
|
|
|
@ -7,9 +7,7 @@ using namespace KYSDK_FILEWATCHER;
|
|||
int main(int argc, char *argv[])
|
||||
{
|
||||
KYSDK_FILEWATCHER::FileWatcher watcher;
|
||||
// watcher.addWatchTarget("/data/testcode/test.c", ONESHOT, MODIFY|OPEN|CLOSE);
|
||||
// watcher.addWatchTarget("/data/testcode/dir_watcher", PERIODIC, OPEN|MODIFY|MOVE|CLOSE|ACCESS|CREATE|DELETE);
|
||||
watcher.addWatchTargetRecursive("/home/kylin/桌面", PERIODIC, ALL, 2);
|
||||
watcher.addWatchTargetRecursive("/home/kylin/", PERIODIC, ALL, 3, REGULAR);
|
||||
|
||||
getchar();
|
||||
|
||||
|
|
Loading…
Reference in New Issue