Without loss of generality let $\theta\in(0,\pi/2)$, otherwise $\theta\to 2\pi-\theta$ would transform the integral into one with the same value (or minus its value for $\theta\to\pi-\theta$).
As there was stated earlier, the integral does not converge, so we are forced to compute only its principal value which is defined as :
$$\mathrm{PV}\int_0^\pi\frac{\mathrm{d}x}{\cos{\theta}-\cos{x}}=\lim_{\varepsilon\to0}
\left[\int_0^{\theta-\varepsilon}\frac{\mathrm{d}x}{\cos{\theta}-\cos{x}}+
\int_{\theta+\varepsilon}^\pi\frac{\mathrm{d}x}{\cos{\theta}-\cos{x}}\right]$$
Making the substitution $x\to\pi-x$ in the second integral (for the simplicity we will not write the limit sign in the next section) :
$$I=\int_0^{\theta-\varepsilon}\frac{\mathrm{d}x}{\cos{\theta}-\cos{x}}-
\int_0^{\pi-\theta-\varepsilon}\frac{\mathrm{d}x}{\cos{(\pi-\theta)}-\cos{x}}$$
So $I=\lim_{\varepsilon\to0}F(\theta-\varepsilon)-G(\pi-\theta-\varepsilon)$ where $F$ and $G$ are primitive function of the first (second) integrands.
Since
$$F(x)=\int\frac{\mathrm{d}x}{\cos{\theta}-\cos{x}}=\int\frac{\frac{2}{1+t^2}\;\mathrm{d}t}{\cos{\theta}-\frac{1-t^2}{1+t^2}}=2\int\frac{\;\mathrm{d}t}{\cos{\theta}(1+t^2)-(1-t^2)}=\\
\int\frac{\;\mathrm{d}t}{t^2\cos^2{\frac{\theta}{2}}-\sin^2{\frac{\theta}{2}}}=-\frac{2}{\sin\theta}\operatorname{arctanh}\frac{\tan\frac{x}{2}}{\tan\frac{\theta}{2}}$$
For $x\in(0,\theta)$. For $\theta\to\pi-\theta$ we get $G$ :
$$G(x)=-\frac{2}{\sin\theta}\operatorname{arctanh}\frac{\cot\frac{\pi-x}{2}}{\cot\frac{\theta}{2}}$$
Therefore
$$I = -\frac{2}{\sin\theta}\lim_{\varepsilon\to 0}\left[\operatorname{arctanh}\frac{\tan\frac{\theta-\varepsilon}{2}}{\tan\frac{\theta}{2}}-\operatorname{arctanh}\frac{\cot\frac{\theta+\varepsilon}{2}}{\cot\frac{\theta}{2}}\right]$$
Since $$\operatorname{arctanh}x=\frac{1}{2}\ln\frac{1+x}{1-x}$$
$$I=-\frac{1}{\sin\theta}\lim_{\varepsilon\to 0}\ln\left[
\frac{\tan\frac{\theta}{2}+\tan\frac{\theta-\varepsilon}{2}}{\tan\frac{\theta}{2}-\tan\frac{\theta-\varepsilon}{2}}
\frac{\cot\frac{\theta}{2}-\cot\frac{\theta+\varepsilon}{2}}{\cot\frac{\theta}{2}+\cot\frac{\theta+\varepsilon}{2}}\right]$$
Next we use sum formulas for $\tan$ and $\cot$ functions, so
$$I=-\frac{1}{\sin\theta}\lim_{\varepsilon\to 0}\ln\left[
\frac{\sin\left(\frac{\theta}{2}+\frac{\theta-\varepsilon}{2}\right)}{\sin\left(\frac{\theta}{2}-\frac{\theta-\varepsilon}{2}\right)}
\frac{\sin\left(\frac{\theta+\varepsilon}{2}-\frac{\theta}{2}\right)}{\sin\left(\frac{\theta+\varepsilon}{2}+\frac{\theta}{2}\right)}\right]$$
With the substitution $\varepsilon=2\delta$
$$I=-\frac{1}{\sin\theta}\lim_{\delta\to 0}\ln\left[
\frac{\sin\left(\theta-\delta\right)}{\sin\delta}
\frac{\sin\delta}{\sin\left(\theta+\delta\right)}\right]=-\frac{1}{\sin\theta}\lim_{\delta\to 0}\ln
\frac{\sin\left(\theta-\delta\right)}{\sin\left(\theta+\delta\right)}=0$$