For context, I was reading this Math.SE answer which is a solution to the integral $I=\int_0^{\infty} \frac{\ln \cos^2 x}{x^2} \,dx=-\pi$ and was confused when reading this part:
$$ 2I = \int_{-\infty}^\infty\frac{\ln(\cos^2x)}{x^2} \,dx = \color{red}{\sum_{n=-\infty}^{+\infty}} \left(\color{red}{\int_{n\pi}^{(n+1)\pi}} \frac{\ln(\cos^2x)}{x^2} \,dx\right). $$
My question is, when does the following hold? $$\int_{-\infty}^\infty f(x) \,dx = \sum_{n=-\infty}^\infty \int_{n\pi}^{(n+1)\pi} f(x) \,dx$$
Thank you.