コード例 #1
0
ファイル: utility.c プロジェクト: ne0ndrag0n/romble-md
inline void Debug_sprint( char* format, ... ) {
	va_list args;
	va_start( args, format );

	char string[ 100 ];
	vsprintf( string, format, args );
	Debug_print( string );

	va_end( args );
}
コード例 #2
0
ファイル: frac.c プロジェクト: Gubberblump/COSC407
int main(void) {
   char command;
   frac_t frac;
   unsigned other_frac, test_eq, u1, u2;

   MPI_Init(NULL, NULL);
   frac = Alloc_frac();

   printf("Enter a command (a, r, e, d, s, q)\n");
   scanf(" %c", &command);
   while (command != 'q') {
      switch(command) {
         case 'a':
            printf("Enter log denom of a frac\n");
            scanf("%u", &other_frac);
            Add(frac, other_frac);
            Print_frac(frac, 0, "Sum = ");
            break;
         case 'r':
            Reduce(frac);
            Print_frac(frac, 0, "Reduced frac = ");
            break;
         case 'e':
            printf("Enter an unsigned int\n");
            scanf("%u", &test_eq);
            if (Equals(frac, test_eq))
               printf("They're equal\n");
            else
               printf("They're not equal\n");
            break;
         case 'd':
            Debug_print(frac);
            break;
         case 's':  // Assign
            printf("Enter two unsigned ints\n");
            scanf("%u %u", &u1, &u2);
            Assign(frac, u1, u2);
            Print_frac(frac, 0, "We created = ");
            break;
         default:
            printf("I didn't understand %c\n", command);
            break;
      }  /* switch */
      printf("Enter a command (a, r, e, d, s, q)\n");
      scanf(" %c", &command);
   }  /* while */

   Free_frac(frac);
   MPI_Finalize();

   return 0;
}  /* main */
コード例 #3
0
ファイル: OperationLog.c プロジェクト: xuqi1987/18.Car
int FileExist(const char *comname,ST_OptLogCfgData *cfg)
{
    DIR *dir;
    struct dirent *dirent;
    if (!comname || !(*comname) || !cfg)
    {
        return -1;
    }
    if (access(cfg->rcdpath,F_OK))
    {
         if (CreateDir(cfg->rcdpath)<0)
         {
             Debug_print("create directory %s for Record file failed",cfg->rcdpath);
             return -1;
         }
    }
    dir = opendir(cfg->rcdpath);
    if (!dir)
    {
        perror("open directory failed");
        return -1;
    }
    while((dirent=readdir(dir))!=NULL)
    {
        if(strstr(dirent->d_name,comname))
        {
            Debug_print("get dirent name :%s\n",dirent->d_name);
            pthread_mutex_lock(&gendata.pathmutex);
            snprintf(gendata.path,PATHLEN,"%s/%s",cfg->rcdpath,dirent->d_name);
            pthread_mutex_unlock(&gendata.pathmutex);
            closedir(dir);
            return 1;
        }
    }
    closedir(dir);
    return 0; 
}
コード例 #4
0
ファイル: OperationLog.c プロジェクト: xuqi1987/18.Car
int GetComps(char *comlist,ST_OptLogCfgData *cfg)
{
    char *str;
    char *prech;
    int slen;
    int i = 0;
    if (!comlist || !cfg)
    {
        return -1;
    }
    prech = comlist;
    do
    {
		if(!prech)    //hukongwang check pointer NULL:14867
		{
			perror("prech is null!\n");	
       		return -1;
    	}
        str = strchr(prech,',');
        if (prech)
        {
            if (str)
            {
                *str = 0;
            }
            slen = strlen(prech);
            ltrim(prech,&slen);
            rtrim(prech,&slen);
            //cfg->comp[i] = prech;
            if (slen <= PATHLEN)
            {
                memcpy(cfg->comp[i],prech,slen);
                i++;
            }
            
            if (i>=256)
            {
                Debug_print("component list is full\n");
                return -1;
            }
            prech = str+1;
        }
    }while(str);
    cfg->compnum = i;
    return 0;
}
コード例 #5
0
ファイル: OperationLog.c プロジェクト: xuqi1987/18.Car
int OptLogInit()
{
    int ret;
    int cfglen;
    int i;
    int j;
    setlinebuf(stdout);
    EngModePrintOn=0;
    ST_OptLogCfgData sval;
    ST_OptLogCfgInfo *cfginf = NULL;
    bzero(&sval,sizeof(sval));
    
    Debug_print("%s\n",CONFIG_FILE);

	if (gendata.initlized)
	{
		Debug_print("The operation log has been initlized ,can not init again\n");
		return 0;
	}
    ret = ReadCfg(CONFIG_FILE,&cfginf,&cfglen);
    if (ret < 0)
    {
        printf("read configuration file failed, use default set\n");
        sval.curlevel = 3;
        sval.enflags |= PrtAllComp;
        if (!getcwd(sval.rcdpath,PATHLEN)) 
        {
            perror("get current directory failed");
            return -1;
        }
    }
    for (i=0;i<cfglen;i++)
    {
        Debug_print("key:%s\t val:%s\n",cfginf[i].key,cfginf[i].val); 
        if (!strcmp(cfginf[i].key,"prtlevel")&&cfginf[i].val[0]!='0')
        {
	    	sval.enflags |= EnLevel; 
            continue;
        }
        if (!strcmp(cfginf[i].key,"prtfilename")&&cfginf[i].val[0]!='0')
        {
	    	sval.enflags|= EnFName; 
            continue;
        }
        if (!strcmp(cfginf[i].key,"prtfuncname")&&cfginf[i].val[0]!='0')
        {
	    	sval.enflags|= EnFcName; 
            continue;
        }
        if (!strcmp(cfginf[i].key,"prtlinenum")&&cfginf[i].val[0]!='0')
        {
	    	sval.enflags|= EnLine; 
            continue;
        }
        if (!strcmp(cfginf[i].key,"prttime")&&cfginf[i].val[0]!='0')
        {
	    	sval.enflags|= EnTime; 
            continue;
        }
        if (!strcmp(cfginf[i].key,"prtinfile")&&cfginf[i].val[0]!='0')
        {
	    	sval.enflags|= EnFRcd; 
            continue;
        }
        if (!strcmp(cfginf[i].key,"recordpath")&&cfginf[i].val[0]!=0)
        {
            int slen;
            slen = strlen(cfginf[i].val);
            if (cfginf[i].val[slen-1] == '/')
            {
                cfginf[i].val[slen-1] = 0;
            }
	    	strncpy(sval.rcdpath,cfginf[i].val,PATHLEN);
            continue;
        }
        if (!strcmp(cfginf[i].key,"level")&&cfginf[i].val[0]!=0)
        {
            sval.curlevel = atoi(cfginf[i].val);
            continue;
        }
        if (!strcmp(cfginf[i].key,"componentlist")&&cfginf[i].val[0]!=0)
        {
            if (GetComps(cfginf[i].val,&sval) < 0)
            {
                Debug_print("get components list failed\n");
            }
            else
            {
                for (j=0;j<sval.compnum;j++)
                Debug_print("component name:%s\n",sval.comp[j]);
            }
            continue;
        }
         
    }
    if (cfginf)
    {
        free(cfginf);
    }
    Debug_print("enflag:%x\tpath:%s\n",sval.enflags,sval.rcdpath);
    //create shared memory and init semaphore
    gendata.shmid = shmget(IPC_KEY,sizeof(ST_OptLogCfgData),0666);
    if (gendata.shmid == -1)
    {
        Debug_print("shared memory does not exist,need create\n");
        gendata.shmid = shmget(IPC_KEY,sizeof(ST_OptLogCfgData),0666|IPC_CREAT);
        if (gendata.shmid < 0)
        {
            perror("create shared memory failed");
            return -1;
        }
        Debug_print("create shared memory success\n");
    } 
    else
    {
        sval.enflags |= SMemExist;
    }
    Debug_print("get shared memory success\n");
    gendata.cfg = (ST_OptLogCfgData *)shmat(gendata.shmid,NULL,0);
    if (gendata.cfg == (void *)-1)
    {
        perror("get shared memory address failed");
        shmctl(gendata.shmid,IPC_RMID,NULL);
        gendata.cfg = NULL;
        return -1;
    }
    //bzero(gendata.cfg,4096*65);

    Debug_print("get shared memory address success\n");

    gendata.semid = semget(SEM_KEY,0,0666);
    if (gendata.semid < 0)
    {
        Debug_print("semaphore is not created, need create\n");
        gendata.semid = semget(SEM_KEY,1,0666|IPC_CREAT);
        if (gendata.semid < 0)
        {
            perror("create semaphore failed");
            return -1;
        }
        Debug_print("create semaphore success\n");
        if (init_sem(gendata.semid,1) < 0)
        {
            printf("init semaphore failed\n");
            del_sem(gendata.semid);
            return -1;
        }
        Debug_print("init semaphore success\n");
    }
    Debug_print("get semaphore success\n");
    if(pthread_mutex_init(&gendata.filemutex,NULL) != 0)
    {
        perror("create file mutex failed");
        return -1;
    }
    if(pthread_mutex_init(&gendata.pathmutex,NULL) != 0)
    {
        perror("create path mutex failed");
        return -1;
    }
    #ifdef READ_CFG
    if (1) 
    #else
    if (!(sval.enflags&SMemExist))
    #endif
    {
        Debug_print("shared memory does not exists need to set value to memory\n");
        Debug_print("curlevel:%d\trcdpath:%s\tcomnum:%d\tenflags:%x\n",sval.curlevel,\
        sval.rcdpath,sval.compnum,sval.enflags);
        OptLogSet(&sval);
    }
	gendata.initlized = 1;
    return 0;
}
コード例 #6
0
ファイル: OperationLog.c プロジェクト: xuqi1987/18.Car
int OptLogSave(ST_OptLogCfgData *val)
{
    FILE *fd;
    char key[100];
    int slen;
    int i;
    int bufofst = 0;
    char buf[2048] = {0};
    char bufstr[256];
    char *tmpptr;
    char *substr;
    fd = fopen(CONFIG_FILE,"r+");
    if (!fd)
    {
        perror("Open configuration file failed");
        return -1;
    }
    slen = 0;
    bzero(buf,sizeof(buf)); 
    while(fgets(bufstr,256-slen,fd))
    {
        tmpptr = bufstr;
        while(isblank(*tmpptr))
        {
            tmpptr++;
        }
        if (*tmpptr=='#'||*tmpptr=='\n')
        {
            tmpptr[strlen(tmpptr)-1] = 0;
            goto storedata;
        }
        Debug_print("get content:%s\n",bufstr);
        substr = strchr(bufstr,'=');
        if (!substr)
        {
            goto storedata;
        }
        bzero(key,100);
        memcpy(key,tmpptr,substr-tmpptr);
        slen = strlen(key);
        rtrim(key,&slen);
        for(i=0; i<slen; i++)
        {
            key[i] = tolower(key[i]);
        }
        substr++;
        while(isblank(*substr)&&*substr!='\n')
        {
            substr++;
        }
        bzero(substr,256-(substr-bufstr));
        if (!strcmp(key,"recordpath"))
        {
            Debug_print("get rcdpath:%s##\n",val->rcdpath);
            memcpy(substr,val->rcdpath,strlen(val->rcdpath));
        }
        else if (!strcmp(key,"prtlevel"))
        {
            *substr = (val->enflags&EnLevel)?'1':'0';
        }
        else if (!strcmp(key,"prtfilename"))
        {
            *substr = (val->enflags&EnFName)?'1':'0';
        }
        else if (!strcmp(key,"prtfuncname"))
        {
            *substr = val->enflags&EnFcName?'1':'0';
        }
        else if (!strcmp(key,"prtlinenum"))
        {
            *substr = val->enflags&EnLine?'1':'0';
        }
        else if (!strcmp(key,"prttime"))
        {
            *substr = (val->enflags&EnTime)?'1':'0';
        }
        else if (!strcmp(key,"prtinfile"))
        {
            *substr = val->enflags&EnFRcd?'1':'0';
        }
        else if (!strcmp(key,"level"))
        {
            *substr = val->curlevel+48;
        }
        else if (!strcmp(key,"componentlist"))
        {
            slen = 0;
            Debug_print("strlen substr is ############## %d\n",strlen(substr));
            bzero(substr,strlen(substr));
            for (i=0; i<val->compnum; i++)
            {
                snprintf(substr+slen,256-(substr-bufstr),"%s,",val->comp[i]);
                slen = strlen(substr);
            }
            Debug_print("component list:%s",substr);
            substr[slen-1] = 0;
        }
        Debug_print("the finale buf is:%s\n",bufstr);
	storedata:
        snprintf(buf+bufofst,1024-bufofst,"%s\n",bufstr);
        bufofst = strlen(buf);
    }
    Debug_print("final content:%s",buf);
    rewind(fd);
    truncate(CONFIG_FILE,0);
    if (fwrite(buf,strlen(buf),1,fd)!=1)
    {
        perror("write data failed");
		fclose(fd);
        return -1; 
    }
    fclose(fd);
    return 0;
}
コード例 #7
0
ファイル: OperationLog.c プロジェクト: xuqi1987/18.Car
int ReadCfg(char *name,ST_OptLogCfgInfo **cfginf,int *inflen)
{
    ST_OptLogCfgInfo *tmp = NULL;
    FILE *file;
    int slen;
    int i;
    int vallen;
    char comflag = 0;
    char *substr;
    char buf[1024];

    if (!name || !cfginf || !inflen)
    {
        return -1;
    }
    *cfginf = NULL;
    *inflen = 0;
    file = fopen(name,"r");
    if (!file)
    {
        printf("Open config file %s failed\n",name);
        return -1;
    }
    while(fgets(buf,sizeof(buf),file)!= NULL)
    {
        slen = strlen(buf)+1;
        ltrim(buf,&slen);    		//trim blank characters on the left
	if (*buf=='#'||*buf=='\n')    	//ignore blank line and comment line
	{
            if (*buf == '#')
            {
                comflag = 0;
            }
    	    continue;
	}
        if (comflag)
        {
            vallen = strlen((*cfginf+*inflen-1)->val);
            Debug_print("get complist buf:%s",buf);
            if (vallen + slen > 8192)
            {
                printf("ERR:the component list is too long, the max long is 8192 characters\n");
            }
            else
            {
	        bcopy(buf,(*cfginf+*inflen-1)->val+vallen,slen);
            }
            continue;
        }
	substr = strchr(buf,'=');
	if (!substr)    	//if can't find equal sign then ignore current line
	{
	    continue;
	}
    //here we get a useful line
	*inflen = *inflen + 1;
	tmp = *cfginf;    	//store previous pointer to tmp
	*cfginf = (ST_OptLogCfgInfo *)malloc(*inflen*sizeof(ST_OptLogCfgInfo));    //alloc a new memory area
	if (!*cfginf)
	{
	    if (tmp)
	    {
	        free(tmp);
	    }
	    fclose(file);
            *inflen = 0;
	    return -1;
	}
	if (tmp)
	{
	    memcpy(*cfginf,tmp,(*inflen-1)*sizeof(ST_OptLogCfgInfo));
	    free(tmp);
	}
	*substr = 0;    		//substitute equal sign to '\0';
	slen = strlen(buf);
        for (i=0;i<slen;i++)
        {
            buf[i] = tolower(buf[i]);
        }
		rtrim(buf,&slen);
		bcopy(buf,(*cfginf+*inflen-1)->key,slen+1);
		slen = strlen(substr+1);
		ltrim(substr+1,&slen);
		rtrim(substr+1,&slen);
		bcopy(substr+1,(*cfginf+*inflen-1)->val,slen+1);
        if(!strcmp((*cfginf+*inflen-1)->key,"componentlist"))
        {
            comflag = 1;
        }
    }
    for (i=0; i<1024; i++)
    {
        if((*cfginf+*inflen-1)->val[i] == '\n')
        {
            (*cfginf+*inflen-1)->val[i] = ' ';
        }
    }
    fclose(file);
    return 0;
}
コード例 #8
0
ファイル: OperationLog.c プロジェクト: xuqi1987/18.Car
int printlog(int level,const char *com,const char *func,const char *file,const char *funcname,int line,const char *fmt,...)
{
    va_list args;
    //ST_OptLogCfgData gval;
    struct tm *ltime;
	struct timeval tv;
    int len = 0;
    int i;
    time_t tim;
    char buf[1024]={0};
    char tmbuf[16] = {0};
  
	if (gendata.initlized != 1)
	{
		printf("(from %s) The operation log has not been initlized,please call OptLogInit to initlize\n",com?com:"unknown component");
		return 0;
	}
    OptLogGet(&gval);
    Debug_print("get cfg from memory and compnum is:%d\n",gval.compnum);
    Debug_print("get enflags:%x\n",gval.enflags);
    if (level < gval.curlevel)
    {
        return 0;
    }
    if (!(gval.enflags&PrtAllComp))
    {
        for(i=0;i<gval.compnum;i++)
        {
            Debug_print("com:%s\n",gval.comp[i]);
            if (!strcmp(gval.comp[i],com))
            {
                Debug_print("match current component\n");
                break;
            }
        }
        if (i == gval.compnum)
        {
            return 0;
        }
    }
    if ((gval.enflags&EnFRcd) && !FileExist(com,&gval))
    {
        Debug_print("record file does not exist neet create\n");
        tim = time(NULL);
        ltime = localtime(&tim);
        if (!ltime)
        {
            perror("get local time failed");
            return -1;
        }
        strftime(tmbuf,16,"%G%m%d_%H%M",ltime);
        pthread_mutex_lock(&gendata.pathmutex);
        snprintf(gendata.path,PATHLEN,"%s/DebugInfo_%s_%s.txt",gval.rcdpath,com,tmbuf);
        pthread_mutex_unlock(&gendata.pathmutex);
        if (gendata.fd)
        {
            close(gendata.fd);
        }
        gendata.fd = open(gendata.path,O_CREAT|O_WRONLY,openflag);
        if (gendata.fd < 0)
        {
            perror("create record file failed");
            return -1;
        }
    }
    Debug_print("get record file name:%s\n",gendata.path);
    if ((gval.enflags&EnFRcd)&&!gendata.fd)
    {
        gendata.fd = open(gendata.path,O_WRONLY,openflag);
        if (gendata.fd < 0)
        {
            perror("Open record file failed");
            return -1;
        }
    }
    if (gval.enflags & EnTime)
    {
        tim = time(NULL);
        ltime = localtime(&tim);
		gettimeofday(&tv,NULL);
        if (!ltime)
        {
            perror("get local time failed");
            return -1;
        }
        strftime(tmbuf,16,"%T",ltime);
		snprintf(buf,1024,"[%s.%ld]",tmbuf,tv.tv_usec);
        len = strlen(buf);
    }
    if (gval.enflags & EnLevel)
    {
        snprintf(buf+len,1024-len,"[level%d]",level);
        len = strlen(buf);
    }
    snprintf(buf+len,1024-len,"[%s]",com);
    len = strlen(buf);
    snprintf(buf+len,1024-len,"[%s]",func);
    len = strlen(buf);
    if (gval.enflags & EnFName)
    {
        snprintf(buf+len,1024-len," in %s",file);
        len = strlen(buf);
    }
    if ((gval.enflags&EnLine)&&(gval.enflags&EnFName))
    {
        snprintf(buf+len,1024-len," line %d",line);
        len = strlen(buf);
    }
    if (gval.enflags & EnFcName)
    {
        snprintf(buf+len,1024-len,"%s",funcname);
        len = strlen(buf);
    }
    snprintf(buf+len,1024-len,": ");   
    len = strlen(buf);
    va_start(args,fmt);
    vsnprintf(buf+len,1024-len,fmt,args);
    va_end(args);

	if(buf[strlen(buf)-1] != '\n' && buf[strlen(buf)-1] != ',')
    { 
		buf[strlen(buf)] = '\n';
	}
  
    if (gval.enflags&EnFRcd)
    {
        pthread_mutex_lock(&gendata.filemutex);
        lseek(gendata.fd,0,SEEK_END);
        if (write(gendata.fd,buf,strlen(buf)) < 0)
        {
            struct statfs fs;
            if (!fstatfs(gendata.fd,&fs) && !fs.f_bavail)
            {
                remove(gendata.path);
            }
        }
        pthread_mutex_unlock(&gendata.filemutex);
    }
    else
    {
	/********optimization algorithm for media[0x**,]**********/
		if(strcmp(com,"YfMedia")==0 && buf[strlen(buf)-1] == ',')
		{
			char *tempbuf = &buf[strlen(buf)-5];
			printf("%s",tempbuf);
		}
	/********optimization algorithm for media[0x**,]**********/
		else
		{		
        	printf("%s",buf);
		}
    }

	if(EngModePrintOn==1)
    {
	pthread_mutex_lock(&gendata.filemutex);
        int EngModefd = open(EngModePath,O_CREAT|O_WRONLY,openflag);
        if (EngModefd < 0)
        {
            perror("create engineeringmode file failed");
			close(EngModefd); 		//check Resource leak
            return -1;
        }
        lseek(EngModefd,0,SEEK_END);
        if(write(EngModefd,buf,strlen(buf)) < 0)
        {
            perror("print engineeringmode file failed");
			close(EngModefd);		//check Resource leak
			return -1;    
        }
	pthread_mutex_unlock(&gendata.filemutex);
	close(EngModefd);  				//check pointer NULL:14881
    }
    Debug_print("end of printlog function\n");
    return 0;
}