When ordered by a court to do so, or when in his opinion it is necessary for the safekeeping of a prisoner, under charge of or sentence for crime, the jailer shall summon a sufficient guard, and the circuit court or other court of record having jurisdiction of criminal cases, in the county in which the jail is, may allow therefor so much as it deems proper, not exceeding $2.50 a day for each man. The court, before certifying any allowance, shall inquire into the condition of the jail. If it appear that such guard was necessary because of the insecurity of the jail, it shall order the allowance to be certified to the county court and it shall be chargeable to the county; but if otherwise, and the guard was necessary, the allowance shall be certified for payment out of the treasury.