Beispiel #1
0
static void
onFileAdded (tr_session * session, const char * dir, const char * file)
{
    char * filename = tr_buildPath (dir, file, NULL);
    tr_ctor * ctor = tr_ctorNew (session);
    int err = tr_ctorSetMetainfoFromFile (ctor, filename);

    if (!err)
    {
        tr_torrentNew (ctor, &err, NULL);

        if (err == TR_PARSE_ERR)
            tr_logAddError ("Error parsing .torrent file \"%s\"", file);
        else
        {
            bool trash = false;
            int test = tr_ctorGetDeleteSource (ctor, &trash);

            tr_logAddInfo ("Parsing .torrent file successful \"%s\"", file);

            if (!test && trash)
            {
                tr_logAddInfo ("Deleting input .torrent file \"%s\"", file);
                if (tr_remove (filename))
                    tr_logAddError ("Error deleting .torrent file: %s", tr_strerror (errno));
            }
            else
            {
                char * new_filename = tr_strdup_printf ("%s.added", filename);
                tr_rename (filename, new_filename);
                tr_free (new_filename);
            }
        }
    }

    tr_ctorFree (ctor);
    tr_free (filename);
}
Beispiel #2
0
void tr_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
{
	if(level>=log_conf.level && log_conf.mode !=TO_NONE)
	{
		char buf[1024];
		char date[64];
		struct tm *tm;
		va_list vp;
		int num;
		time_t current_time=time(NULL);
		tm=localtime(&current_time);
		//snprintf(date,strlen(date)-1,"%d-%d %d:%d:%d",tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
		strftime(date,sizeof(date),"%b %e %T",tm);
		va_start(vp,fmt);
		vsnprintf(buf,sizeof(buf),fmt,vp);
		va_end(vp);
		if(log_conf.mode==TO_SCREEN || log_conf.mode==TO_BOTH)
		{
			fprintf(stderr,"\033[37;40m[%s] %s \033[37m%s()@%s:%d => \033[0;37;40m%s\n\033[0m",date,log_descriptions[level].color,function,file,line,buf);
			fflush(stderr);
		}
		if(log_conf.fp!=NULL)
		{
			num=fprintf(log_conf.fp,"[%s] %s %s()@%s:line %d  %s\n",date,log_descriptions[level].nocolor,function,file,line,buf);
			if(num>0)
			{
				fflush(log_conf.fp);
				log_conf.current+=num;
				if(log_conf.current>=log_conf.limit)
				{
					fclose(log_conf.fp);
					log_conf.fp=NULL;
					if(log_conf.rotate==1){
						int i;
						char old_name[FILE_PATH_LEN];
						char new_name[FILE_PATH_LEN];
						for(i=log_conf.backup;i>=1;i--)
						{
							snprintf(old_name,sizeof(old_name),"%s.bak.%d",log_conf.file_name,i);
							snprintf(new_name,sizeof(new_name),"%s.bak.%d",log_conf.file_name,i+1);
							if(tr_exist(old_name)==0){
								continue;
							}else{
								if(i==log_conf.backup){
									tr_remove(old_name);
									continue;
								}
								tr_rename(old_name,new_name);
							}
						}
						snprintf(new_name,sizeof(new_name),"%s.bak.1",log_conf.file_name);
						tr_rename(log_conf.file_name,new_name);
					}else{
						tr_remove(log_conf.file_name);
					}
					start_log();
				}
			}
		}
	}
}