* 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 int pfd[2];
+static pthread_t loggingThread;
bool iCompare(const std::string& a, const std::string& b)
{
scanFilesInDir(d.c_str(), reader, depth-1);
closedir(dir);
-}
\ No newline at end of file
+}
+
+static void *stdlog(void*)
+{
+ ssize_t readSize;
+ char buf[1024];
+
+ while ((readSize = read(pfd[0], buf, sizeof buf - 1)) > 0) {
+ if (buf[readSize - 1] == '\n') {
+ --readSize;
+ }
+
+ buf[readSize] = 0;
+
+ _INFO("%s", buf);
+ }
+
+ return 0;
+}
+
+int runLoggingThread() { // run this function to redirect your output to android log
+ if (setvbuf(stdout, NULL, _IOLBF, 0) < 0) {
+ _DBG("fail to make stdout line-buffered");
+ return -1;
+ }
+
+ if (setvbuf(stderr, NULL, _IONBF, 0) < 0) {
+ _DBG("make stderr unbuffered");
+ return -1;
+ }
+
+ /* create the pipe and redirect stdout and stderr */
+ if (pipe(pfd) < 0) {
+ _DBG("fail to create pipe for logging");
+ return -1;
+ }
+
+ if (dup2(pfd[1], fileno(stdout)) == -1) {
+ _DBG("fail to duplicate fd to stdout");
+ return -1;
+ }
+
+ if (dup2(pfd[1], fileno(stderr)) == -1) {
+ _DBG("fail to duplicate fd to stderr");
+ return -1;
+ }
+
+ /* spawn the logging thread */
+ if (pthread_create(&loggingThread, 0, stdlog, 0) != 0) {
+ _DBG("fail to create pthread");
+ return -1;
+ }
+
+ if (pthread_detach(loggingThread) != 0) {
+ _DBG("fail to detach pthread");
+ return -1;
+ }
+
+ return 0;
+}
+