test.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <unistd.h>
  2. #include <time.h>
  3. #include <iostream>
  4. #include <thread>
  5. #include <log.h>
  6. #include <dirent.h>
  7. #include <string.h>
  8. #include <fnmatch.h>
  9. void dir_clean(const char*dir,const char*pattern,int count)
  10. {
  11. struct dirent **namelist;
  12. int n = scandir(dir, &namelist, NULL, alphasort);
  13. if (n < 0)
  14. return;
  15. char fname[512];
  16. strcpy(fname,dir);
  17. strcat(fname,"/");
  18. char*p=&fname[strlen(fname)];
  19. for (int i=n-1;i>=0;i--)
  20. {
  21. if(0==fnmatch(pattern,namelist[i]->d_name,0) && count--<=0)
  22. {
  23. strcpy(p,namelist[i]->d_name);
  24. remove(fname);
  25. printf("remove file:%s\n",fname);
  26. }
  27. free(namelist[i]);
  28. }
  29. free(namelist);
  30. }
  31. int main()
  32. {
  33. // dir_clean("/home/zzj/ya-src/dist/log","yals_*.log",3);
  34. // dir_clean("../dist/log","yals_*.log",3);
  35. char *s=(char*)malloc(128);
  36. strcpy(s,"abc,de,,f,g");
  37. char*p=0;
  38. for(;*s;)
  39. p=strtok_r(s,",",&s);
  40. log_init("../etc/log.ini");
  41. log_info("%s","this is a test.");
  42. log_info("%s","I'm lht.");
  43. log_info("%s","I'm zzj.");
  44. std::this_thread::yield();
  45. return 0;
  46. }